Build a circle with openNURBS in C++

Hi,

I am using openNURBS library and C++ and C# programmation to check if a circle is indeed a circle. See :
http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/NURBS/RB-circles.html

I am trying to build the circle inside the triangle.

I obtain very strange results. When asking for a non-rational calculation, what I get seems to fit with what should be ok (see http://geometrie.foretnik.net/files/NURBS-en.swf ), but rational nurbs is not building the circle at all. I hope i haven’t forgotten anything in the definition of the nurbs. Does anyone has a clue?

You will find that openNURBS handles knots differently than your online test application. You might spend a moment reading through this technical article:

Below is how you could create a degree=2 circle using openNURBS in conjunction with the Rhino C++ SDK:

CRhinoCommand::result CCommandBernus::RunCommand(const CRhinoCommandContext& context)
{
  int dimension = 3;
  ON_BOOL32 bIsRational = TRUE;
  int degree = 2;
  int cv_count = 7;
  int knot_count = cv_count + degree - 1;

  ON_SimpleArray<ON_3dPoint> points(cv_count);
  points.SetCount(cv_count);
  points[0] = ON_3dPoint(2.500, 0.000, 0.000);
  points[1] = ON_3dPoint(5.000, 0.000, 0.000);
  points[2] = ON_3dPoint(3.750, 2.165, 0.000);
  points[3] = ON_3dPoint(2.500, 4.330, 0.000);
  points[4] = ON_3dPoint(1.250, 2.165, 0.000);
  points[5] = ON_3dPoint(0.000, 0.000, 0.000);
  points[6] = ON_3dPoint(2.500, 0.000, 0.000);
 
  ON_SimpleArray<double> weights(cv_count);
  weights.SetCount(cv_count);
  weights[0] = 1.0;
  weights[1] = 0.5;
  weights[2] = 1.0;
  weights[3] = 0.5;
  weights[4] = 1.0;
  weights[5] = 0.5;
  weights[6] = 1.0;

  ON_SimpleArray<double> knots(knot_count);
  weights.SetCount(knot_count);
  knots[0] = 0.000;
  knots[1] = 0.000;
  knots[2] = 0.333;
  knots[3] = 0.333;
  knots[4] = 0.667;
  knots[5] = 0.667;
  knots[6] = 1.000;
  knots[7] = 1.000;

  ON_NurbsCurve nc(dimension, bIsRational, degree + 1, cv_count);

  for (int ci = 0; ci < nc.CVCount(); ci++)
  {
    ON_4dPoint p4d(points[ci].x * weights[ci], points[ci].y * weights[ci], points[ci].z * weights[ci], weights[ci]);
    nc.SetCV(ci, p4d);
  }

  for (int ki = 0; ki < knot_count; ki++)
    nc.m_knot[ki] = knots[ki];

  if (nc.IsValid())
  {
    context.m_doc.AddCurveObject(nc);
    context.m_doc.Redraw();
  }

  return CRhinoCommand::success;
}
1 Like

Thank you for your answer.
It works fine indeed.
Now I can have a circle with a Nurbs curve…