# edge rendering issue on mac m1

**URL:** https://discourse.vtk.org/t/edge-rendering-issue-on-mac-m1/14638
**Category:** Development
**Tags:** rendering
**Created:** [September 30, 2024, 7:25pm UTC](https://discourse.vtk.org/t/edge-rendering-issue-on-mac-m1/14638 "2024-09-30T19:25:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![frankham](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/e9bcb4/32.png) [@frankham](https://discourse.vtk.org/u/frankham)
#### Post date: [September 30, 2024, 7:25pm UTC](https://discourse.vtk.org/t/edge-rendering-issue-on-mac-m1/14638/1 "2024-09-30T19:25:17Z")

</div>

I am writing an application that renders edges using simple lines with coloring based on a scalar value and lookup table. On linux and my older intel-based mac the following simple example works as expected, with edge color cycling r-g-b-r-g-b etc around the perimeter of the circle. On mac m1, however, I observe the result that only half the edges get rendered, and each edge (which appears to consist of 2 triangles) gets two different colors in the r-g-b sequence.

![circle_mac_intel](https://discourse.vtk.org/uploads/default/original/2X/d/daa378bdd8332bcd5d6d287ff8cd77dc965620f6.png)  
 ![circle_mac_m1](https://discourse.vtk.org/uploads/default/original/2X/7/7fe1c9ecace0f0b8127778b3e4e490bfbabec0fd.png)

here is the code:

```auto
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkTriangle.h>
#include <vtkActor.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkLine.h>
#include <vtkMath.h>
#include <vtkFloatArray.h>
#include <vtkIntArray.h>
#include <vtkDoubleArray.h>
#include <vtkCellData.h>
#include <vtkLookupTable.h>

int main(int, char *[]) {

  // Create points for the circle's center and perimeter
    const double radius = 1.0;
    const int numSegments = 36;
    double center[3] = {0.0, 0.0, 0.0};
    
    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    points->InsertNextPoint(center);

    // Create points around the circle
    for (int i = 0; i < numSegments; ++i) {
      double angle = 2.0 * vtkMath::Pi() * i / numSegments;
      double x = center[0] + radius * cos(angle);
      double y = center[1] + radius * sin(angle);
      points->InsertNextPoint(x, y, center[2]);
    }
    
    // Create triangles for the circle
    vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();
    for (int i = 1; i <= numSegments; ++i) {
      int next = (i % numSegments) + 1;
      vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();
      triangle->GetPointIds()->SetId(0, 0); // Center point
      triangle->GetPointIds()->SetId(1, i); // Current point on the circle
      triangle->GetPointIds()->SetId(2, next); // Next point on the circle
      triangles->InsertNextCell(triangle);
    }
    
    // Create polydata for the circle
    vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
    polyData->SetPoints(points);
    polyData->SetPolys(triangles);

    // Create a mapper and actor for the circle
    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputData(polyData);

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    // ========================
    // edges...
    // ========================
    
    // Create the edge using lines
    vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();

    // and add a scalar for coloring with a lookup table...
    //vtkSmartPointer<vtkFloatArray> scalars = vtkSmartPointer<vtkFloatArray>::New(); // all array types produce same issue
    vtkSmartPointer<vtkIntArray> scalars = vtkSmartPointer<vtkIntArray>::New();
    //vtkSmartPointer<vtkDoubleArray> scalars = vtkSmartPointer<vtkDoubleArray>::New();
    scalars->SetNumberOfComponents(1);
    scalars->SetName("EdgeScalars");

    for (int i = 1; i <= numSegments; ++i) {
      int next = (i % numSegments) + 1;
      vtkSmartPointer<vtkLine> line = vtkSmartPointer<vtkLine>::New();
      line->GetPointIds()->SetId(0, i);
      line->GetPointIds()->SetId(1, next);
      lines->InsertNextCell(line);
      
      // alternate scalar values 0,1,2,0,1,2...
      scalars->InsertNextValue(i%3);
	
    }

    // Create a polydata for the edge
    vtkSmartPointer<vtkPolyData> edgeData = vtkSmartPointer<vtkPolyData>::New();
    edgeData->SetPoints(points);
    edgeData->SetLines(lines);
    edgeData->GetCellData()->SetScalars(scalars);

    // r,g,b, lookup table...
    vtkSmartPointer<vtkLookupTable> lut = vtkSmartPointer<vtkLookupTable>::New();
    lut->SetNumberOfTableValues(3);
    lut->SetTableValue(0,1.0,0.0,0.0);
    lut->SetTableValue(1,0.0,1.0,0.0);
    lut->SetTableValue(2,0.0,0.0,1.0);
    
    // Create a mapper and actor for the edge
    vtkSmartPointer<vtkPolyDataMapper> edgeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    edgeMapper->SetInputData(edgeData);
    
    edgeMapper->SetScalarRange(0,2);
    edgeMapper->SetLookupTable(lut);
    
    vtkSmartPointer<vtkActor> edgeActor = vtkSmartPointer<vtkActor>::New();
    edgeActor->SetMapper(edgeMapper);

    //edgeActor->GetProperty()->SetColor(1.0, 0.0, 0.0); // Red color for the edge (when not using lut)
    edgeActor->GetProperty()->SetLineWidth(10.0); // Thickened edge

    // Create a renderer, render window, and interactor
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    // Add the actors to the scene
    renderer->AddActor(actor);
    renderer->AddActor(edgeActor);
    renderer->SetBackground(0.1, 0.2, 0.3); // Background color

    // Render...
    renderWindow->Render();

    // interact...
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

```

A couple other clues to this problem: if the edges do not add the scalar for coloring (i.e. no scalar, no lookup table, and we simply color the actor red), all platforms tested produce the correct result, with all edges rendered in red around the full circle.

You can see from the code above that I tried several different types for the edge-based scalar. None of these choices correct the problem (int, double, float).

The issue may be related to the following august post on edge rendering with mac:

> [@Tree edges are rendered incorrectly in vtkCocoa after calling SetLineWidth()](https://discourse.vtk.org/t/tree-edges-are-rendered-incorrectly-in-vtkcocoa-after-calling-setlinewidth/14436):
>
> So there appears to be an obscure bug present in the vtkCocoa renderer (used in MacOS) which happens in vtkGraphLayoutView. Modifying the line width of the graph edges breaks the render. It appears that all rectangles in the graph layout are typically internally treated as two triangles, but something is wrong with the creation of these objects in cocoa in which only one triangle is created per edge. The effect is seen in the screenshot below: In this exam…

Any help would be appreciated, as this issue is preventing us from developing edge highlighting and selection routines for m1,2,3 builds. Thanks!

---

<div class="post-metadata">

### Author: ![fraseyboo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/fraseyboo/32/2411_2.png) [@fraseyboo](https://discourse.vtk.org/u/fraseyboo)
#### Post date: [September 30, 2024, 7:38pm UTC](https://discourse.vtk.org/t/edge-rendering-issue-on-mac-m1/14638/2 "2024-09-30T19:38:55Z")

</div>

This definitely seems related to what I was seeing, I think internally it’s coming from some incorrect conversion from quads (which is how the thick lines are displayed) to triangles. You can likely build the edge cells manually by iterating through the perimeter and adding the two triangles that make up the segment but it’d definitely be better if the code actually worked as expected.

My issue was relatively obscure, yours seems more integral so hopefully it gets more attention.

---

<div class="post-metadata">

### Author: ![frankham](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/e9bcb4/32.png) [@frankham](https://discourse.vtk.org/u/frankham)
#### Post date: [September 30, 2024, 8:15pm UTC](https://discourse.vtk.org/t/edge-rendering-issue-on-mac-m1/14638/3 "2024-09-30T20:15:14Z")

</div>

@fraseyboo as you say, I expect that the issue is pretty deep. Probably the conversion of a thickened line to a polygonal approximation in OpenGL code. On mac with m1, my understanding is that apple does not use native OpenGL anymore, but Metal with OpenGL emulation through MoltenVK. I’d rather not hack a fix by rendering my own tris because it would be difficult to make the line-thickness the same (i.e. a constant pixel dimension) at all zoom levels. This is the problem with other approaches to edge highlighting such as vtkTubeFilter.

---

<div class="post-metadata">

### Author: ![Michael\_Jackson](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michael_jackson/32/61_2.png) [@Michael\_Jackson](https://discourse.vtk.org/u/Michael_Jackson)
#### Post date: [October 2, 2024, 3:40pm UTC](https://discourse.vtk.org/t/edge-rendering-issue-on-mac-m1/14638/4 "2024-10-02T15:40:42Z")

</div>

> [@VTK & Qt: rendering issues when drawing thick lines](https://discourse.vtk.org/t/vtk-qt-rendering-issues-when-drawing-thick-lines/13903/6):
>
> @dkutak What OS and graphics drivers are you running this on?

---

<div class="post-metadata">

### Author: ![frankham](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/e9bcb4/32.png) [@frankham](https://discourse.vtk.org/u/frankham)
#### Post date: [October 3, 2024, 2:01am UTC](https://discourse.vtk.org/t/edge-rendering-issue-on-mac-m1/14638/5 "2024-10-03T02:01:48Z")

</div>

macOS Ventura 13.7 is the one with problems.

---

<div class="post-metadata">

### Author: ![frankham](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/e9bcb4/32.png) [@frankham](https://discourse.vtk.org/u/frankham)
#### Post date: [October 3, 2024, 2:03am UTC](https://discourse.vtk.org/t/edge-rendering-issue-on-mac-m1/14638/6 "2024-10-03T02:03:14Z")

</div>

During the cmake process on this machine, I see the following:  
Found OpenGL: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/System/Library/Frameworks/OpenGL.framework found components: OpenGL
