lunedì 27 aprile 2020

Sort Points by clockwise Angle


    Points[4] = {...};
    Point origin;

...

    // Sort Points by Angles
    for(int m=0; m<3; m++)
        for (int i = m + 1; i < 4; i++)
        {
            if (GetClockwiseAngle(Points[i], origin) < GetClockwiseAngle(Points[m], origin);)
            {               
                SwapPoint(Points, i, m);   
            }
        }
       
...

double GetClockwiseAngle(Point point, Point origin)
{
    double angle = 0.0;
    angle = atan2(point.Y - origin.Y, point.X - origin.X) * 180 / M_PI;;

    return angle;
}

Middle point of a line segment


A(x,y)
B(x,y)

MidAB.X = (A.X + B.X) / 2;
MidAB.Y = (A.Y + B.Y) / 2;

Rotate Point Around Origin

//----------------------------------------------------------------------------
void RotatePointAroundOrigin(&Point, Point origin, double angle)
{
    float s = sin(angle);
    float c = cos(angle);

    // Translate point back to origin:
    point.X -= origin.X;
    point.Y -= origin.Y;

    // Rotate point
    float xnew = point.X * c - point.Y * s;
    float ynew = point.X * s + point.Y * c;

    // Translate point back:
    point.X = xnew + origin.X;
    point.Y = ynew + origin.Y;
}

Angle between two points

//----------------------------------------------------------------------------
double CalculateAngle(Point point1, Point point2, Point origin)
{
    double angleToP1 = atan2((point1.X - origin.X), (point1.Y - origin.Y));
    double angleToP2 = atan2((point2.X - origin.X), (point2.Y - origin.Y));

    double angle = angleToP2 - angleToP1;

    if (angle < 0) angle += (2 * vtkMath::Pi());
   
    return angle;
}

mercoledì 11 marzo 2020

VTK Textured Map Plane + Image Data

Ref: https://vtk.org/Wiki/VTK/Examples/Cxx/Visualization/TextureMapPlane 
Ref: https://vtk.org/Wiki/VTK/Examples/Cxx/Visualization/TextureMapImageData

 ...
   vtkImageCanvasSource2D *imageSource; vtkNEW(imageSource);
    imageSource->SetScalarTypeToUnsignedChar();
    imageSource->SetExtent(0, 10, 0, 10, 0, 0);
    imageSource->SetNumberOfScalarComponents(3);
    imageSource->SetDrawColor(127, 255, 100);
    imageSource->FillBox(0, 10, 0, 10);
    imageSource->SetDrawColor(20, 20, 20);
    imageSource->DrawSegment(0, 0, 9, 9);
    imageSource->DrawSegment(9, 0, 0, 9);
    imageSource->Update();
   
    texture->SetInput(imageSource->GetOutput());


    // Create a plane
    vtkPlaneSource *plane; vtkNEW(plane);
    plane->SetCenter(0.0, 0.0, 0.0);
    plane->SetNormal(0.0, 0.0, -1.0);

    vtkPolyDataMapper *planeMapper; vtkNEW(planeMapper);
    plane->GetOutput()->GetPointData()->SetTCoords(textureCoordinates);
    planeMapper->SetInput(plane->GetOutput());

    vtkActor *texturedPlane; vtkNEW(texturedPlane);
    texturedPlane->SetMapper(planeMapper);
    texturedPlane->SetTexture(texture);

    m_Renderer->AddActor(texturedPlane);

vtk textured polygon

Ref: https://vtk.org/Wiki/VTK/Examples/Cxx/Visualization/TextureMapQuad

// Read the image which will be the texture
  vtkSmartPointer<vtkJPEGReader> jPEGReader =
    vtkSmartPointer<vtkJPEGReader>::New();
  jPEGReader->SetFileName ( inputFilename.c_str() );
  
  // Create a plane
  vtkSmartPointer<vtkPoints> points =
    vtkSmartPointer<vtkPoints>::New();
  points->InsertNextPoint(0.0, 0.0, 0.0);
  points->InsertNextPoint(1.0, 0.0, 0.0);
  points->InsertNextPoint(1.0, 1.0, 0.0);
  points->InsertNextPoint(0.0, 2.0, 0.0);
  
  vtkSmartPointer<vtkCellArray> polygons =
    vtkSmartPointer<vtkCellArray>::New();
  vtkSmartPointer<vtkPolygon> polygon =
    vtkSmartPointer<vtkPolygon>::New();
  polygon->GetPointIds()->SetNumberOfIds(4); //make a quad
  polygon->GetPointIds()->SetId(0, 0);
  polygon->GetPointIds()->SetId(1, 1);
  polygon->GetPointIds()->SetId(2, 2);
  polygon->GetPointIds()->SetId(3, 3);
  
  polygons->InsertNextCell(polygon);
vtk, 
  vtkSmartPointer<vtkPolyData> quad =
    vtkSmartPointer<vtkPolyData>::New();
  quad->SetPoints(points);
  quad->SetPolys(polygons);
  
  vtkSmartPointer<vtkFloatArray> textureCoordinates =
    vtkSmartPointer<vtkFloatArray>::New();
  textureCoordinates->SetNumberOfComponents(3);
  textureCoordinates->SetName("TextureCoordinates");
  
  float tuple[3] = {0.0, 0.0, 0.0};
  textureCoordinates->InsertNextTuple(tuple);
  tuple[0] = 1.0; tuple[1] = 0.0; tuple[2] = 0.0;
  textureCoordinates->InsertNextTuple(tuple);
  tuple[0] = 1.0; tuple[1] = 1.0; tuple[2] = 0.0;
  textureCoordinates->InsertNextTuple(tuple);
  tuple[0] = 0.0; tuple[1] = 2.0; tuple[2] = 0.0;
  textureCoordinates->InsertNextTuple(tuple); 
 
  textureCoordinates->SetNumberOfComponents(2);
  textureCoordinates->InsertTuple2(0, 0.0, 0.0); 
  textureCoordinates->InsertTuple2(1, 1.0, 0.0);
  textureCoordinates->InsertTuple2(2, 1.0, 1.0);
  textureCoordinates->InsertTuple2(3, 0.0, 1.0);
   
 quad->GetPointData()->SetTCoords(textureCoordinates);
  
  // Apply the texture
  vtkSmartPointer<vtkTexture> texture =
    vtkSmartPointer<vtkTexture>::New();
  texture->SetInputConnection(jPEGReader->GetOutputPort());
  
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
  mapper->SetInput(quad);
#else
  mapper->SetInputData(quad);
#endif

  vtkSmartPointer<vtkActor> texturedQuad =
    vtkSmartPointer<vtkActor>::New();
  texturedQuad->SetMapper(mapper);
  texturedQuad->SetTexture(texture);

  // Visualize the textured plane
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(texturedQuad);
  renderer->SetBackground(1,1,1); // Background color white
  renderer->ResetCamera();

venerdì 21 febbraio 2020

Hello world - Progress Bar (Loading...)


Vector2 m_Position = {100, 100};
Vector2 m_Size = {400, 20};


Vector2 m_ProgressBar_Size = m_Size;
int m_Progress = 0;


 // Update m_Progress

 m_Progress_Rectangle.setPosition(m_Position);

 m_ProgressBar_Size.x = (m_Size.x / 100)*m_Progress;
 m_Progress_Rectangle.setSize(m_ProgressBar_Size);

 print("Loading...%d%", m_Progress);