POV-Ray : Newsgroups : povray.general : Impossible stl file conversion! : Re: Impossible stl file conversion! Server Time
15 Sep 2025 08:06:14 EDT (-0400)
  Re: Impossible stl file conversion!  
From: ingo
Date: 10 Sep 2025 08:35:00
Message: <web.68c16f95a068c28c17bac71e8ffb8ce3@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:
> Ilya Razmanov <ily### [at] gmailcom> wrote:
>
> > I'm sorry but I don't get the joke (well, it's only 6:26 here and I'm
> > still waiting for my coffee to extract).
>
> It's now 6:24 here, and I'm in the exact same situation.  :D
>
> > So?
>
> It's another installment in the long-running desire to have native stl support
> for POV-Ray.  There was some insistence that .stl is a binary-only format, or
> that it would be difficult to adapt our mesh {} code to handle stl.
>
> https://news.povray.org/web.65818958d9619d9a1f9dae3025979125%40news.povray.org
>
> So the point is that if the SDL and stl are virtually identical, then it ought
> to be fairly simple to add the ability _in source_ to #include "MyModel.stl"
>
> - BE

#declare VertexVec = array[100] {
    <8,0,0> <9.44122,11.8709,0> <2.70223,19.3541,0>
    <-8.09808,18.1865,0> <-13.0913,7.48319,0> <-9.58918,-5.47232,0>
    <-5.40192,-13.8564,0> <-3.63688,-15.7569,0> <-0.826068,-10.2846,0>
    <4,-3.91887e-15,0> <7.53209,0,1.28558> <8.99892,11.4998,1.58634>
    <2.55384,18.5125,2.34791> <-7.51319,17.1735,3.21394>
<-11.7988,7.01274,3.7792>
    <-8.21366,-5.47232,3.7792> <-4.23215,-13.8564,3.21394>
<-2.7823,-15.7569,2.34791>
    <-0.248686,-10.2846,1.58634> <4.46791,-3.91887e-15,1.28558>
<6.3473,0,1.96962>
    <7.87898,10.5601,2.43042> <2.17809,16.3816,3.59721>
<-6.0322,14.6083,4.92404>
    <-8.52589,5.82151,5.79006> <-4.73074,-5.47232,5.79006>
<-1.27016,-13.8564,4.92404>
    <-0.618457,-15.7569,3.59721> <1.2133,-10.2846,2.43042>
<5.6527,-3.91887e-15,1.96962>
    <5,0,1.73205> <6.60543,9.49143,2.13727> <1.75081,13.9583,3.16333>
    <-4.34808,11.6913,4.33013> <-4.80411,4.46689,5.0917>
<-0.7701,-5.47232,5.0917>
    <2.09808,-13.8564,4.33013> <1.84218,-15.7569,3.16333>
<2.8758,-10.2846,2.13727>
    <7,-3.91887e-15,1.73205> <4.12061,0,0.68404> <5.77417,8.79393,0.844075>
    <1.47192,12.3767,1.2493> <-3.24884,9.78742,1.7101>
<-2.37489,3.58272,2.01087>
    <1.81502,-5.47232,2.01087> <4.29654,-13.8564,1.7101>
<3.44825,-15.7569,1.2493>
    <3.96092,-10.2846,0.844075> <7.87939,-3.91887e-15,0.68404>
<4.12061,0,-0.68404>
    <5.77417,8.79393,-0.844075> <1.47192,12.3767,-1.2493>
<-3.24884,9.78742,-1.7101>
    <-2.37489,3.58272,-2.01087> <1.81502,-5.47232,-2.01087>
<4.29654,-13.8564,-1.7101>
    <3.44825,-15.7569,-1.2493> <3.96092,-10.2846,-0.844075>
<7.87939,-3.91887e-15,-0.68404>
    <5,0,-1.73205> <6.60543,9.49143,-2.13727> <1.75081,13.9583,-3.16333>
    <-4.34808,11.6913,-4.33013> <-4.80411,4.46689,-5.0917>
<-0.7701,-5.47232,-5.0917>
    <2.09808,-13.8564,-4.33013> <1.84218,-15.7569,-3.16333>
<2.8758,-10.2846,-2.13727>
    <7,-3.91887e-15,-1.73205> <6.3473,0,-1.96962> <7.87898,10.5601,-2.43042>
    <2.17809,16.3816,-3.59721> <-6.0322,14.6083,-4.92404>
<-8.52589,5.82151,-5.79006>
    <-4.73074,-5.47232,-5.79006> <-1.27016,-13.8564,-4.92404>
<-0.618457,-15.7569,-3.59721>
    <1.2133,-10.2846,-2.43042> <5.6527,-3.91887e-15,-1.96962>
<7.53209,0,-1.28558>
    <8.99892,11.4998,-1.58634> <2.55384,18.5125,-2.34791>
<-7.51319,17.1735,-3.21394>
    <-11.7988,7.01274,-3.7792> <-8.21366,-5.47232,-3.7792>
<-4.23215,-13.8564,-3.21394>
    <-2.7823,-15.7569,-2.34791> <-0.248686,-10.2846,-1.58634>
<4.46791,-3.91887e-15,-1.28558>
    <8,0,-4.89859e-16> <9.44122,11.8709,-6.04464e-16>
<2.70223,19.3541,-8.94654e-16>
    <-8.09808,18.1865,-1.22465e-15> <-13.0913,7.48319,-1.44003e-15>
<-9.58918,-5.47232,-1.44003e-15>
    <-5.40192,-13.8564,-1.22465e-15> <-3.63688,-15.7569,-8.94654e-16>
<-0.826068,-10.2846,-6.04464e-16>
    <4,-3.91887e-15,-4.89859e-16>
  }

#declare NormalVec = array[100] {
    <0.970398,-0.241511,0> <0.964518,0.264016,0> <0.338788,0.940863,0>
    <-0.600834,0.799374,0> <-0.99802,0.0629003,0> <-0.940787,-0.338999,0>
    <-0.865497,-0.500914,-1.16282e-16> <0.615314,-0.788282,6.5167e-17>
<0.899878,-0.436142,5.59569e-17>
    <0.961129,-0.2761,0> <0.751627,-0.193101,0.63069>
<0.782153,0.176031,0.597703>
    <0.306739,0.711273,0.632456> <-0.439771,0.628692,0.641364>
<-0.779063,0.0397155,0.625686>
    <-0.734709,-0.283085,0.616494> <-0.686809,-0.44291,0.576301>
<0.50335,-0.753823,-0.422361>
    <0.712277,-0.368035,-0.597672> <0.748144,-0.214917,-0.627767>
<0.173444,-0.0485249,0.983648>
    <0.250024,-0.0325129,0.967694> <0.212934,0.135699,0.967597>
<0.00249393,0.200893,0.97961>
    <-0.173347,0.0312182,0.984366> <-0.172656,-0.106756,0.97918>
<-0.167243,-0.269104,0.948479>
    <0.13505,-0.628609,-0.765906> <0.171168,-0.168396,-0.970744>
<0.173433,-0.0498214,-0.983585>
    <-0.494052,0.153785,0.855723> <-0.477882,-0.198973,0.855592>
<0.0681164,-0.513488,0.855389>
    <0.552115,-0.217784,0.804822> <0.557043,0.152347,0.816391>
<0.494296,0.150612,0.856147>
    <0.499687,-0.035358,0.865484> <-0.460527,-0.389434,-0.797657>
<-0.498203,0.0846973,-0.862914>
    <-0.494921,0.142174,-0.857228> <-0.896097,0.301054,0.326153>
<-0.927261,-0.21252,0.308257>
    <-0.0705502,-0.937584,0.340527> <0.888954,-0.364892,0.276793>
<0.911329,0.307808,0.273375>
    <0.885942,0.33336,0.322456> <0.932562,0.122959,0.339425>
<-0.926214,-0.168767,-0.337114>
    <-0.911208,0.244351,-0.331652> <-0.90722,0.260614,-0.330201>
<-0.896097,0.301054,-0.326153>
    <-0.927261,-0.21252,-0.308257> <-0.0705502,-0.937584,-0.340527>
<0.888954,-0.364892,-0.276793>
    <0.911329,0.307808,-0.273375> <0.885942,0.33336,-0.322456>
<0.932562,0.122959,-0.339425>
    <-0.926214,-0.168767,0.337114> <-0.911208,0.244351,0.331652>
<-0.90722,0.260614,0.330201>
    <-0.494052,0.153785,-0.855723> <-0.477882,-0.198973,-0.855592>
<0.0681164,-0.513488,-0.855389>
    <0.552115,-0.217784,-0.804822> <0.557043,0.152347,-0.816391>
<0.494296,0.150612,-0.856147>
    <0.499687,-0.035358,-0.865484> <-0.460527,-0.389434,0.797657>
<-0.498203,0.0846973,0.862914>
    <-0.494921,0.142174,0.857228> <0.173444,-0.0485249,-0.983648>
<0.250024,-0.0325129,-0.967694>
    <0.212934,0.135699,-0.967597> <0.00249393,0.200893,-0.97961>
<-0.173347,0.0312182,-0.984366>
    <-0.172656,-0.106756,-0.97918> <-0.167243,-0.269104,-0.948479>
<0.13505,-0.628609,0.765906>
    <0.171168,-0.168396,0.970744> <0.173433,-0.0498214,0.983585>
<0.751627,-0.193101,-0.63069>
    <0.782153,0.176031,-0.597703> <0.306739,0.711273,-0.632456>
<-0.439771,0.628692,-0.641364>
    <-0.779063,0.0397155,-0.625686> <-0.734709,-0.283085,-0.616494>
<-0.686809,-0.44291,-0.576301>
    <0.50335,-0.753823,0.422361> <0.712277,-0.368035,0.597672>
<0.748144,-0.214917,0.627767>
    <0.970398,-0.241511,0> <0.964518,0.264016,-6.83606e-16>
<0.338788,0.940863,0>
    <-0.600834,0.799374,-9.79107e-17> <-0.99802,0.0629003,-2.37935e-16>
<-0.940787,-0.338999,-4.55888e-16>
    <-0.865497,-0.500914,-2.55819e-16> <0.615314,-0.788282,2.3623e-16>
<0.899878,-0.436142,2.47809e-16>
    <0.961129,-0.2761,3.22825e-16>
  }

#declare FacesIndices = array[162]{
    <0,1,11> <10,0,11> <1,2,12> <11,1,12> <2,3,13> <12,2,13>
    <3,4,14> <13,3,14> <4,5,15> <14,4,15> <5,6,16> <15,5,16>
    <6,7,17> <16,6,17> <7,8,18> <17,7,18> <8,9,19> <18,8,19>
    <10,11,21> <20,10,21> <11,12,22> <21,11,22> <12,13,23> <22,12,23>
    <13,14,24> <23,13,24> <14,15,25> <24,14,25> <15,16,26> <25,15,26>
    <16,17,27> <26,16,27> <17,18,28> <27,17,28> <18,19,29> <28,18,29>
    <20,21,31> <30,20,31> <21,22,32> <31,21,32> <22,23,33> <32,22,33>
    <23,24,34> <33,23,34> <24,25,35> <34,24,35> <25,26,36> <35,25,36>
    <26,27,37> <36,26,37> <27,28,38> <37,27,38> <28,29,39> <38,28,39>
    <30,31,41> <40,30,41> <31,32,42> <41,31,42> <32,33,43> <42,32,43>
    <33,34,44> <43,33,44> <34,35,45> <44,34,45> <35,36,46> <45,35,46>
    <36,37,47> <46,36,47> <37,38,48> <47,37,48> <38,39,49> <48,38,49>
    <40,41,51> <50,40,51> <41,42,52> <51,41,52> <42,43,53> <52,42,53>
    <43,44,54> <53,43,54> <44,45,55> <54,44,55> <45,46,56> <55,45,56>
    <46,47,57> <56,46,57> <47,48,58> <57,47,58> <48,49,59> <58,48,59>
    <50,51,61> <60,50,61> <51,52,62> <61,51,62> <52,53,63> <62,52,63>
    <53,54,64> <63,53,64> <54,55,65> <64,54,65> <55,56,66> <65,55,66>
    <56,57,67> <66,56,67> <57,58,68> <67,57,68> <58,59,69> <68,58,69>
    <60,61,71> <70,60,71> <61,62,72> <71,61,72> <62,63,73> <72,62,73>
    <63,64,74> <73,63,74> <64,65,75> <74,64,75> <65,66,76> <75,65,76>
    <66,67,77> <76,66,77> <67,68,78> <77,67,78> <68,69,79> <78,68,79>
    <70,71,81> <80,70,81> <71,72,82> <81,71,82> <72,73,83> <82,72,83>
    <73,74,84> <83,73,84> <74,75,85> <84,74,85> <75,76,86> <85,75,86>
    <76,77,87> <86,76,87> <77,78,88> <87,77,88> <78,79,89> <88,78,89>
    <80,81,91> <90,80,91> <81,82,92> <91,81,92> <82,83,93> <92,82,93>
    <83,84,94> <93,83,94> <84,85,95> <94,84,95> <85,86,96> <95,85,96>
    <86,87,97> <96,86,97> <87,88,98> <97,87,98> <88,89,99> <98,88,99>
  }

#version 3.7;


// STL export macro
#macro WriteSTL(filename)
  #fopen STLFile filename write

  // Write STL header
  #write(STLFile, "solid POVRayMesh\n")

  // Get the number of faces
  #local NumFaces = dimension_size(FacesIndices, 1);

  // Loop through all faces
  #local i = 0;
  #while (i < NumFaces)
    // Get the three vertex indices for this face
    #local v1_idx = FacesIndices[i].x;
    #local v2_idx = FacesIndices[i].y;
    #local v3_idx = FacesIndices[i].z;

    // Get the vertices
    #local v1 = VertexVec[v1_idx];
    #local v2 = VertexVec[v2_idx];
    #local v3 = VertexVec[v3_idx];

    // Calculate face normal (cross product of two edge vectors)
    #local edge1 = v2 - v1;
    #local edge2 = v3 - v1;
    #local face_normal = vnormalize(vcross(edge1, edge2));

    // Write facet to STL file
    #write(STLFile, "  facet normal ", face_normal.x, " ", face_normal.y, " ",
face_normal.z, "\n")
    #write(STLFile, "    outer loop\n")
    #write(STLFile, "      vertex ", v1.x, " ", v1.y, " ", v1.z, "\n")
    #write(STLFile, "      vertex ", v2.x, " ", v2.y, " ", v2.z, "\n")
    #write(STLFile, "      vertex ", v3.x, " ", v3.y, " ", v3.z, "\n")
    #write(STLFile, "    endloop\n")
    #write(STLFile, "  endfacet\n")

    #local i = i + 1;
  #end

  // Write STL footer
  #write(STLFile, "endsolid POVRayMesh\n")

  #fclose STLFile
#end

// Call the macro to write the STL file
WriteSTL("mesh_output.stl")

// Optional: Display a message
#debug "STL file 'mesh_output.stl' has been created.\n"

// Create a simple scene to render (optional)
camera {
  location <20, 10, -20>
  look_at <0, 0, 0>
}

light_source {
  <30, 30, -30>
  color rgb <1, 1, 1>
}

// Render the mesh for visualization (optional)
mesh2 {
  vertex_vectors {
    dimension_size(VertexVec, 1),
    #local i = 0;
    #while (i < dimension_size(VertexVec, 1))
      VertexVec[i]
      #if (i < dimension_size(VertexVec, 1) - 1) , #end
      #local i = i + 1;
    #end
  }

  normal_vectors {
    dimension_size(NormalVec, 1),
    #local i = 0;
    #while (i < dimension_size(NormalVec, 1))
      NormalVec[i]
      #if (i < dimension_size(NormalVec, 1) - 1) , #end
      #local i = i + 1;
    #end
  }

  face_indices {
    dimension_size(FacesIndices, 1),
    #local i = 0;
    #while (i < dimension_size(FacesIndices, 1))
      FacesIndices[i]
      #if (i < dimension_size(FacesIndices, 1) - 1) , #end
      #local i = i + 1;
    #end
  }

  pigment { color rgb <0.8, 0.6, 0.4> }
  finish { ambient 0.2 diffuse 0.8 }
}


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.