How to color nurbs surface using Rhino C++ API

I have a nurbs surface that I created in C++ with its m_color attribute set to red. In Shaded view with Flat shading enabled this results in coloring the edges, the isocurves and a patch near the center but not the entire surface:
image

Is there a way to make the whole surface one color? Or should I use a different Rhino Object to do this job?

Goal: I want this surface to set on top of a non-planar mesh face (the surface is made from the face’s vertices) and the color will indicate the degree of non-planarity.

Regards,
Terry.

It would be a good idea to assign this topic to the Developer category.

@dale,

This is an old problem from what I gather from past Forum posts. Any thoughts about how I can achieve this? Should I try to go the route of a custom Visual Analysis Mode to achieve this? By the way, your links to creating a custom Visual Analysis Mode back in 2014 or so are now broken.

Regards,
Terry.

You have me at a disadvantage. What past posts?

Perhaps you can share some source that that isn’t working the way you want?

Thanks,

— Dale

Search the Forum for Visual Analysis Mode. The first hit is one of your posts. It was March 2017 not 2014.

The useful sounding link: https://github.com/mcneel/Rhino5Samples_CPP/tree/master/SampleAnalysisMode
cannot be found.

@dale ,
Can you dig up something that will help with implementing a custom Visual Analysis Mode?

Regrds,
Terry.

@Terry_Chappell the readme for that repository tells you the samples are moved to the rhino-developer-samples repository.

Thanks for providing the link to the SampleAnalysisMode example. From reading it I gathered that it provides automation for coloring the vertices of a mesh that overlays your object. Nice for Z-analysis etc.

No simple, direct way to uniformly color a nurbs surface is revealed. Like for a CurveObject:

ON_3dmObjectAttributes attribs;
pDoc->GetDefaultObjectAttributes(attribs);
attribs.m_color.SetRGB(0, 255, 0);
auto pattribs = &attribs;
pDoc->AddCurveObject(ON_Line(pt0, pt1), pattribs)

Regards,
Terry.

Please post a 3dm with that surface. I don’t understand the small shaded patch in the middle.

Steve,

I used this code to try and add a nurbs surface on top of non-planar quads in the mesh. The mesh vertices pt0, pt1, pt2, pt3 are used to create the nurbs surface.

ON_NurbsSurface nurbs_surface;
int dimension = 3; // 3D surface
bool is_rational = true; // Rational surface
int order0 = 2; // Order in the U direction
int order1 = 2; // Order in the V direction
int cv_count0 = 2; // Number of control points in the U direction
int cv_count1 = 2; // Number of control points in the V direction
bool good = nurbs_surface.Create(dimension, is_rational, order0, order1, cv_count0, cv_count1);
nurbs_surface.SetCV(0, 0, pt0); nurbs_surface.SetCV(1, 0, pt1);
nurbs_surface.SetCV(1, 1, pt2); nurbs_surface.SetCV(0, 1, pt3);
sattribs.m_color = scolor;
c4 = pDoc->AddSurfaceObject(nurbs_surface, spattribs);
if (!c4) nb++;
else ng++;

The number of non-planar quads in this mesh is 493. Only 196 of these get a nurbs surface added (ng) while 297 (nb) do not. I do no know why this is happening. Also the faces that get an added nurbs surface is not reproducible as shown in the 3 successive results below:




Those images are all for Wireframe View. In Shaded View the last one looks like:

Notice 2 of the faces are completely colored in. I have no idea why but this is what I want for all the non-planar faces.

I have not been able to reproduce my earlier picture with the black rectangle colored red.

The non-planar quads are outlined in green using other code that traces over the edges of the mesh face with thick green lines.

Here is the .3dm showing the case with a few solid colored faces:

2D Nurbs Surface on top of non-planar quads.3dm (2.4 MB)

I have virtually no experience with nurbs surfaces. I would not be surprised if my bit of code above has issues. Something is definitely off as less than 50% of the faces are getting nurbs surfaces and the result is not reproducible from run to run. There’s trouble in River City and it starts with Nurbs…

Regards,
Terry,

@Terry_Chappell the U and V domains are incorrect. Here I isolated one of those surfaces and ran the _What command:

Then I ran the _Reparameterize command and chose Automatic

I believe you need to use SetDomain for U and V. Probably from 0 to the length of your U and V, I’d start with distance between pt0 and pt1, and pt1 and pt2. 0 (U?), and 1 (V?) for the dir on the subsequent setting of the domains.

Note, I’m not too familiar with the NURBS stuff, I’m sure others will correct any misconceptions I had. But maybe this is a good start for you.

I added a U & V SetDomain statement and now get a better result:


I turned off Show isocurves and show surface edges in Shaded View to clean up the coloring.

Still failing to generate a nurbs surface for the majority of the non-planar mesh faces which are outlined in Green. Maybe its an issue with the numbers I am putting into the SetDomain statements.

Thanks for your help in getting me this far.

Regards,
Terry.

Maybe try the RhinoCreateSurfaceFromCorners function for this.

This produces a much, much better result:


Now all the non-planar faces (Green outline) have a colored nurbs surface.

Very nice.

Fooling around with:

nurbs_surface.Create(dimension, is_rational, order0, order1, cv_count0, cv_count1)

for quad faces is overkill, messier and unreliable.

Thanks for the lift.

Regards,
Terry.

@Steve, @nathanletwory,

I used the height of each non-planar quad to color its nurbs surface using:

// Raise vertex points by 0.015 in direction of face normal to put surface above mesh.|
pt0 = pt0 + 0.015 * fn; pt1 = pt1 + 0.015 * fn; pt2 = pt2 + 0.015 * fn; pt3 = pt3 + 0.015 * fn;|
ON_NurbsSurface* nurbs_surface = RhinoCreateSurfaceFromCorners(pt0, pt1, pt2, pt3);
ON_Color scolor.SetHSV(2. * ON_PI * ((ht - min_ht) / range), 1.0, 1.0);
attribs.m_color = scolor;
c4 = pDoc->AddSurfaceObject(*nurbs_surface, &attribs);
delete nurbs_surface;

and got my desired result for highlighting the usage of non-planar quads in this mesh:

The highlighting quickly shows where this design deviates from bilateral symmetry.
For example, here is the top-left corner:

While this is the top-right corner:

This shows the top-right corner using flatter non-planar quads that the left.

It is challenging to display the nurbs surfaces on top of the mesh as the mesh can protrude above the surface. Initially this was pretty bad until I raised the nurbs surface above its mesh face by 0.015 in the direction of the face normal. Even with this, you can still see the mesh breaking thru the nurbs surface of the blue face in the top-left corner.

Coloring the mesh directly would avoid this issue but would bleed color into adjacent faces. Adding a denser mesh on top of each mesh face would help the coloring issue but could still have bleed thru problems when displaying the original mesh. This could be fixed by replacing the entire original mesh and coloring the not non-planar faces grey. What I am doing now is good enough for my current purposes and very simple, with the help of Steve’s shortcut, adding only 6 lines of code (see above) to my existing code for outlining the non-planar faces.

Regards,
Terry.