[vtkusers] How to extract area of (triangle) Cells?

David Doria daviddoria+vtk at gmail.com
Wed Dec 16 19:32:57 EST 2009


On Wed, Dec 16, 2009 at 7:19 PM, David Doria <daviddoria+vtk at gmail.com> wrote:
> On Wed, Dec 16, 2009 at 5:16 PM,  <lynx.abraxas at freenet.de> wrote:
>> Hello!
>>
>>
>> I'd  need  to  get the area of the cells (triangle) to weight the cell normals
>> (as discussed in the thread below).
>> My research on this lead me to http://www.cmake.org/Wiki/VTK_Examples where it
>> says that CellData needs an associated data (eg. area of triangle). Would that
>> be GetCellData().GetScalars()? Or would I need to apply a  filter  first  that
>> actually calculates the cell area or would I have to do that my self?
>> I also found this example:
>> http://www.cmake.org/Wiki/Add_Miscellaneous_Data_to_Cells_in_a_Polydata
>> but the area there seems to me very arbritrary;-)
>>
>> Thanks for any help or hints.
>> Lynx
>
>
> I think you want something like this:
>
>  for(unsigned int i = 0; i < polydata->GetNumberOfCells(); i++)
>    {
>    vtkCell* triangle = polydata->GetCell(0);
>    cout << "tri: " << *triangle << endl;
>    //vtkTriangle* triangle = dynamic_cast<vtkTriangle*>(cell);
>    double area =
> vtkTriangle::TriangleArea(triangle->GetPoints()->GetPoint(0),
> triangle->GetPoints()->GetPoint(1),
> triangle->GetPoints()->GetPoint(2));
>
>    cout << "area of triangle " << i << ": " << area << endl;
>    }
>
> I tried it quickly and it says the area of the demo triangles I made
> was 0, so there is something wrong, but it may help you get started.
> I'll play with it more later tonight if you don't get it by then.
>
> Does anyone know if there is a more direct way like
> triangle->ComputeArea(), or if not, can we add one?
>
> Thanks,
>
> David
>

There is something wrong with the GetPoint() function that returns a
pointer (or at least my understanding of how it works).

It works when you do it like this:
http://www.cmake.org/Wiki/VTK/Examples/Triangle_Area

Let me know if this is what you were trying to do. If there is not a
cleaner way to do this, there definitely should be. I couldn't imagine
trying to teach a new user how to use VTK and them asking me something
as simple as finding the area of a triangle and it is 50 lines of code
to demonstrate.

Thanks,

David



More information about the vtkusers mailing list