home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Freeware 25 / FreelogHS25.iso / Dessin / ArtOfIllusion2.2.1 / Scripts / Tools / Thicken.bsh < prev   
Text File  |  2005-06-11  |  3KB  |  121 lines

  1. /*
  2. <?xml version='1.0' standalone='yes' ?>
  3.  
  4. <script>
  5.     <name>Thicken</name>
  6.     <author>Peter Eastman (peastman@users.sourceforge.net)</author>
  7.     <version>1.8</version>
  8.     <date>06/24/2004</date>
  9.     <description>
  10. This script gives "thickness" to meshes by extruding the surface outward into a solid object.
  11.     </description>
  12. </script>
  13. */
  14.  
  15. // Find the object to modify.
  16.  
  17. scene = window.getScene();
  18. sel = scene.getSelection();
  19. if (sel.length != 1 || !(scene.getObject(sel[0]).object instanceof TriangleMesh))
  20. {
  21.   new MessageDialog(window, "Please select a single triangle mesh to solidify.");
  22.   return;
  23. }
  24. info = scene.getObject(sel[0]);
  25. mesh = info.object;
  26.  
  27. // Ask the user to select a thickness.
  28.  
  29. thicknessField = new ValueField(0.1, ValueField.POSITIVE);
  30. dlg = new ComponentsDialog(window, "Select the thickness for "+info.name,
  31.     new Widget [] {thicknessField}, new String [] {"Thickness"});
  32. if (!dlg.clickedOk())
  33.   return;
  34. thickness = thicknessField.getValue();
  35.  
  36. // Duplicate and outset the vertices.
  37.  
  38. vert = mesh.getVertices();
  39. norm = mesh.getNormals();
  40. newVert = new TriangleMesh.Vertex [vert.length*2];
  41. for (i = 0; i < vert.length; i++)
  42. {
  43.   newVert[i] = new TriangleMesh.Vertex(mesh, vert[i]);
  44.   newVert[i+vert.length] = new TriangleMesh.Vertex(mesh, vert[i]);
  45.   newVert[i].r.add(norm[i].times(thickness));
  46. }
  47.  
  48. // Count the boundary edges.
  49.  
  50. edge = mesh.getEdges();
  51. numBoundary = 0;
  52. for (i = 0; i < edge.length; i++)
  53.   if (edge[i].f2 == -1)
  54.     numBoundary++;
  55.  
  56. // Duplicate the faces.
  57.  
  58. face = mesh.getFaces();
  59. newFace = new int [face.length*2+numBoundary*2][3];
  60. for (i = 0; i < face.length; i++)
  61. {
  62.   newFace[i][0] = face[i].v1;
  63.   newFace[i][1] = face[i].v2;
  64.   newFace[i][2] = face[i].v3;
  65.   newFace[face.length+i][0] = face[i].v1+vert.length;
  66.   newFace[face.length+i][1] = face[i].v3+vert.length;
  67.   newFace[face.length+i][2] = face[i].v2+vert.length;
  68. }
  69.  
  70. // Add edges around the boundary.
  71.  
  72. for (i = 0, j = face.length*2; i < edge.length; i++)
  73. {
  74.   if (edge[i].f2 == -1)
  75.   {
  76.     e = edge[i];
  77.     f = face[e.f1];
  78.     newFace[j][0] = e.v2;
  79.     newFace[j][1] = e.v1;
  80.     newFace[j++][2] = e.v1+vert.length;
  81.     newFace[j][0] = e.v2;
  82.     newFace[j][1] = e.v1+vert.length;
  83.     newFace[j++][2] = e.v2+vert.length;
  84.   }
  85. }
  86.  
  87. // Set the new list of vertices and faces for the mesh.
  88.  
  89. mesh.setShape(newVert, newFace);
  90. info.clearCachedMeshes();
  91.  
  92. // Calling setShape() rebuilds the list of edges from scratch, which means that all edge smoothness
  93. // values are lost.  We therefore need to copy them back over.
  94.  
  95. newf = mesh.getFaces();
  96. newe = mesh.getEdges();
  97. for (i = 0; i < face.length; i++)
  98. {
  99.   newe[newf[i].e1].smoothness = edge[face[i].e1].smoothness;
  100.   newe[newf[i].e2].smoothness = edge[face[i].e2].smoothness;
  101.   newe[newf[i].e3].smoothness = edge[face[i].e3].smoothness;
  102.   newe[newf[i+face.length].e1].smoothness = edge[face[i].e3].smoothness;
  103.   newe[newf[i+face.length].e2].smoothness = edge[face[i].e2].smoothness;
  104.   newe[newf[i+face.length].e3].smoothness = edge[face[i].e1].smoothness;
  105. }
  106.  
  107. // Set the edges around the boundary of the original mesh to have a smoothness of 0, so that
  108. // we get a sharp corner.
  109.  
  110. firstNewFace = face.length*2;
  111. for (i = 0; i < newe.length; i++)
  112. {
  113.   j = 0;
  114.   if (newe[i].f1 >= firstNewFace)
  115.     j++;
  116.   if (newe[i].f2 >= firstNewFace)
  117.     j++;
  118.   if (j == 1)
  119.     newe[i].smoothness = 0.0f;
  120. }
  121.