Hi Warren,

I took your advice and modified the RayRenderVRML2()
function to support triangles. I also threw out
the sphere code that was already there and rewrote it
(the code there was for VRML1, not VRML2). While I was at
it, I also implemented export for cylinders and sausages.

The code in the attached patch (diff-ed against cvs,
and tested with two VRML2 readers) can be regarded as
being in the public domain.

Regards,
Chris

--
 ____________________________________________________________________
( Chris Want                                                         )
( Research Computing Support                                         )
( Academic Information and Communication Technologies (AICT)         )
( University of Alberta                                              )
( Tel: 1-780-492-9418                                                )
 --------------------------------------------------------------------

Warren DeLano wrote:
Chris,

There is actually some nascent VRML2/WRL rendering code in the current
CVS.  Right now the only exported rendition is spheres, but the code is
almost at a point where it could export triangles too.  Note that

save test.wrl

already works from the command line (with spheres visible).
If you want to help, please look in layer1/Ray.c for the RayRenderVRML2
methoed and the cPrimTriangle case statement.  Compare that to the
cPrimTriangle case statement in RayRenderPOV.  You might be able to
adapt that code to output VRML instead of Povray commands.

By the way, for us to incorporate changes made directly to PyMOL source
files, you need to either (1) grant explicit transfer of ownership to
us, or (2) place your code into the public domain.  In contrast,
standalone modules which don't alter PyMOL source files can remain under
your ownership provided that they are placed under (3) a
PyMOL-compatible open source license (a modified "BSD" license).
Unfortunately, we cannot accept contibutions placed under any version of
the GNU public license (GPL) although we can accept standalone libraries
under the Lesser GNU public license (LGPL).

Sorry to burden everyone with the legal details, but it is important to
understand things before spending too much time & energy on PyMOL code
modifications.  Please go forward with open eyes and informed
expectations.  We do need more good developers working on PyMOL!

Cheers,
Warren

PS. Examples source code file headers:

Case (1):

/* On January 28th, 2006, I, John Doe, hereby transfer copyright and
assign all rights, title, and interest in the following source code to
DeLano Scientific LLC. */
Case (2):

/* On January 28th, 2006, I, John Doe, hereby place the following source
code into the public domain. */

Case (3): /* ______module name___________ Copyright Notice
==================================

The _____module name_________ source code is copyrighted, but you can
freely
use and copy it as long as you don't change or remove any of the
Copyright notices.  ________module name_______ is made available
under the following open-source license terms:

----------------------------------------------------------------------
______module name_____ is Copyright (C) 2006 _____your name_________

                        All Rights Reserved

Permission to use, copy, modify, distribute, and distribute modified
versions of this software and its built-in documentation for any
purpose and without fee is hereby granted, provided that the above
copyright notice appears in all copies and that both the copyright
notice and this permission notice appear in supporting documentation,
and that the names of ___your name and company (if any)____ not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

_____your name and company (if any)______ DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ______your name
and company (if any)__________ BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
----------------------------------------------------------------------
*/


--
Warren L. DeLano, Ph.D. Principal Scientist

. DeLano Scientific LLC . 400 Oyster Point Blvd., Suite 213 . South San Francisco, CA 94080 USA . Biz:(650)-872-0942 Tech:(650)-872-0834 . Fax:(650)-872-0273 Cell:(650)-346-1154 . mailto:war...@delsci.com
Index: layer1/Ray.c
===================================================================
RCS file: /cvsroot/pymol/pymol/layer1/Ray.c,v
retrieving revision 1.191
diff -u -p -r1.191 Ray.c
--- layer1/Ray.c        27 Jan 2006 03:17:29 -0000      1.191
+++ layer1/Ray.c        7 Feb 2006 21:27:47 -0000
@@ -867,65 +867,254 @@ void RayRenderVRML2(CRay *I,int width,in
   char *vla = *vla_ptr;
   int cc = 0; /* character count */
   OrthoLineType buffer;
+
   
   RayExpandPrimitives(I);
   RayTransformFirst(I,0);
 
-  strcpy(buffer,"#VRML V1.0 ascii\n\n");
-  UtilConcatVLA(&vla,&cc,buffer);
-
-  UtilConcatVLA(&vla,&cc,"MaterialBinding { value OVERALL }\n");
-
-  sprintf(buffer,"Material {\n ambientColor 0 0 0\n diffuseColor 1 1 1\n 
specularColor 1 1 1\nshininess 0.2\n}\n");
+  strcpy(buffer,"#VRML V2.0 ascii\n\n");
   UtilConcatVLA(&vla,&cc,buffer);
 
   { 
-    int a;
+    int a, b;
     CPrimitive *prim;
     float *vert;
-    CBasis *base = I->Basis+1;
+    int mesh_obj = false, mesh_start;
 
-    UtilConcatVLA(&vla,&cc,"Separator {\n");
-
-  UtilConcatVLA(&vla,&cc,"MatrixTransform {\n");
-  UtilConcatVLA(&vla,&cc,"matrix 1.0 0.0 0.0 0.0\n");
-  UtilConcatVLA(&vla,&cc,"       0.0 1.0 0.0 0.0\n");
-  UtilConcatVLA(&vla,&cc,"       0.0 0.0 1.0 0.0\n");
-  sprintf(buffer,"    %8.6f %8.6f %8.6f 1.0\n",
-          (I->Volume[0]+I->Volume[1])/2,
-          (I->Volume[2]+I->Volume[3])/2,0.0F);
-  UtilConcatVLA(&vla,&cc,buffer);
-  UtilConcatVLA(&vla,&cc,"}\n");
+    CBasis *base = I->Basis+1;
 
     for(a=0;a<I->NPrimitive;a++) {
       prim = I->Primitive+a;
       vert = base->Vertex+3*(prim->vert);
+
+      if(prim->type==cPrimTriangle) {
+        if(!mesh_obj) {
+          /* start mesh */
+          mesh_start = a;
+          UtilConcatVLA(&vla,&cc, 
+                        "Shape {\n"
+                        " geometry IndexedFaceSet {\n"
+                        "  coord Coordinate {\n"
+                        "   point [\n");
+          mesh_obj=true;
+        }
+      } else if(mesh_obj) {
+        int tri = 0;
+        /* output connectivity */
+        UtilConcatVLA(&vla,&cc, 
+                      "   ]\n"
+                      "  }\n"
+                      "  coordIndex [\n");
+        for(b=mesh_start;b<a;b++) {
+          sprintf(buffer,"%d %d %d -1,\n", tri, tri+1, tri+2);
+          UtilConcatVLA(&vla,&cc,buffer);        
+          tri+=3;
+        }
+
+        /* output vertex colors */
+        UtilConcatVLA(&vla,&cc, 
+                      "  ]\n"
+                      "  colorPerVertex TRUE\n"
+                      "  color Color {\n"
+                      "   color [\n");
+        for(b=mesh_start;b<a;b++) {
+          CPrimitive *cprim;
+          cprim = I->Primitive+b;
+          sprintf(buffer,
+                  "%6.4f %6.4f %6.4f,\n"
+                  "%6.4f %6.4f %6.4f,\n"
+                  "%6.4f %6.4f %6.4f,\n", 
+                  cprim->c1[0],cprim->c1[1],cprim->c1[2],
+                  cprim->c2[0],cprim->c2[1],cprim->c2[2],
+                  cprim->c3[0],cprim->c3[1],cprim->c3[2]);
+          UtilConcatVLA(&vla,&cc,buffer);
+        }
+
+        /* close mesh */
+        UtilConcatVLA(&vla,&cc,
+                      "   ]\n"
+                      "  }\n"
+                      " }\n"
+                      "}\n");
+        mesh_obj=false;
+      }
+
       switch(prim->type) {
       case cPrimSphere:
         sprintf(buffer,
-                "Material {\ndiffuseColor %6.4f %6.4f %6.4f\n}\n\n", 
+                "Transform {\n"
+                " translation %8.6f %8.6f %8.6f\n"
+                " children Shape {\n"
+                "  geometry Sphere { radius %8.6f }\n"
+                "  appearance Appearance {\n"
+                "   material Material { diffuseColor %6.4f %6.4f %6.4f }\n"
+                "  }\n"
+                " }\n"
+                "}\n",        
+                vert[0],vert[1],vert[2]-z_corr,
+                prim->r1,
                 prim->c1[0],prim->c1[1],prim->c1[2]);
         UtilConcatVLA(&vla,&cc,buffer);    
-        UtilConcatVLA(&vla,&cc,"Separator {\n");
-        sprintf(buffer,
-                "Transform {\ntranslation %8.6f %8.6f %8.6f\nscaleFactor %8.6f 
%8.6f %8.6f\n}\n",
-                vert[0],vert[1],vert[2]-z_corr,         
-                prim->r1,prim->r1,prim->r1);
-        UtilConcatVLA(&vla,&cc,buffer);    
-        sprintf(buffer,"Sphere {}\n");
-        UtilConcatVLA(&vla,&cc,buffer);
-        UtilConcatVLA(&vla,&cc,"}\n\n");        
         break;
       case cPrimCylinder:
-        break;
       case cPrimSausage:
+        {
+          float *d, vert2[3], axis[3], angle;
+          OrthoLineType geometry;
+          /* find the axis and angle that will rotate the y axis onto
+           * the direction of the length of the cylinder
+           */
+          d=base->Normal+3*base->Vert2Normal[prim->vert];
+          if ((d[0]*d[0] + d[2]*d[2]) < 0.000001) {
+            /* parallel with y */
+            axis[0] = 1.0;
+            axis[1] = 0.0;
+            axis[2] = 0.0;
+            if (d[1] > 0) {
+              angle = 0.0;
+            }
+            else {
+              angle = cPI;
+            }
+          }
+          else {
+            axis[0] =  d[2];
+            axis[1] =  0.0;
+            axis[2] = -d[0];
+            normalize3f(axis);
+            angle = d[1];
+            if (angle > 1.0) angle = 1.0;
+            else if (angle < -1.0) angle = -1.0;
+            angle = acos(angle);
+          }
+          /* vrml cylinders have origin in middle, not tip, that is why we
+           * use prim->l1/2
+           */
+          scale3f(d,prim->l1/2,vert2);
+          add3f(vert,vert2,vert2);
+          if (prim->type==cPrimSausage) {
+            sprintf(geometry,
+                    "  Shape {\n"
+                    "   geometry Cylinder {\n"
+                    "    radius %8.6f\n"
+                    "    height %8.6f\n"
+                    "    bottom FALSE\n"
+                    "    top    FALSE\n"
+                    "   }\n"
+                    "   appearance Appearance {\n"
+                    "    material Material { diffuseColor %6.4f %6.4f %6.4f 
}\n"
+                    "   }\n"
+                    "  }\n"
+                    "  Transform {\n"
+                    "   translation 0.0 %8.6f 0.0\n"
+                    "   children Shape {\n"
+                    "    geometry Sphere { radius %8.6f }\n"
+                    "    appearance Appearance {\n"
+                    "     material Material { diffuseColor %6.4f %6.4f %6.4f 
}\n"
+                    "    }\n"
+                    "   }\n"
+                    "  }\n"
+                    "  Transform {\n"
+                    "   translation 0.0 %8.6f 0.0\n"
+                    "   children Shape {\n"
+                    "    geometry Sphere { radius %8.6f }\n"
+                    "    appearance Appearance {\n"
+                    "     material Material { diffuseColor %6.4f %6.4f %6.4f 
}\n"
+                    "    }\n"
+                    "   }\n"
+                    "  }\n", 
+                    prim->r1, prim->l1,
+                    (prim->c1[0]+prim->c2[0])/2,
+                    (prim->c1[1]+prim->c2[1])/2,
+                    (prim->c1[2]+prim->c2[2])/2,
+                    prim->l1/2, prim->r1,  
+                    prim->c1[0],prim->c1[1],prim->c1[2],
+                    -prim->l1/2, prim->r1,  
+                    prim->c2[0],prim->c2[1],prim->c2[2]);
+          }
+          else {
+            sprintf(geometry,
+                    "  Shape {\n"
+                    "   geometry Cylinder {\n"
+                    "    radius %8.6f\n"
+                    "    height %8.6f\n"
+                    "   }\n"
+                    "   appearance Appearance {\n"
+                    "    material Material { diffuseColor %6.4f %6.4f %6.4f 
}\n"
+                    "   }\n"
+                    "  }\n",
+                    prim->r1, prim->l1,
+                    (prim->c1[0]+prim->c2[0])/2,
+                    (prim->c1[1]+prim->c2[1])/2,
+                    (prim->c1[2]+prim->c2[2])/2);
+          }
+          sprintf(buffer,
+                  "Transform {\n"
+                  " translation %8.6f %8.6f %8.6f\n"
+                  " rotation %8.6f %8.6f %8.6f %8.6f\n"
+                  " children [\n"
+                  "%s"
+                  " ]\n"
+                  "}\n", 
+                  vert2[0], vert2[1], vert2[2]-z_corr,
+                  axis[0], axis[1], axis[2], angle,
+                  geometry);
+          UtilConcatVLA(&vla,&cc,buffer);
+        }
         break;
       case cPrimTriangle:
+        /* output coords. connectivity and vertex colors handled above/below */
+        sprintf(buffer,
+                "%8.6f %8.6f %8.6f,\n    %8.6f %8.6f %8.6f,\n    %8.6f %8.6f 
%8.6f,\n", 
+                vert[0], vert[1], vert[2]-z_corr,
+                vert[3], vert[4], vert[5]-z_corr,
+                vert[6], vert[7], vert[8]-z_corr);
+        UtilConcatVLA(&vla,&cc,buffer);    
         break;
       }
     }
 
-    UtilConcatVLA(&vla,&cc,"}\n");
+    if(mesh_obj) {
+      int tri = 0;
+      /* output connectivity */
+      UtilConcatVLA(&vla,&cc, 
+                    "   ]\n"
+                    "  }\n"
+                    "  coordIndex [\n");
+      for(b=mesh_start;b<a;b++) {
+        sprintf(buffer,
+                "%d %d %d -1,\n", tri, tri+1, tri+2);
+        UtilConcatVLA(&vla,&cc,buffer);
+        tri+=3;
+      }
+
+      /* output vertex colors */
+      UtilConcatVLA(&vla,&cc,
+                    "  ]\n"
+                    "  colorPerVertex TRUE\n"
+                    "  color Color {\n"
+                    "   color [\n");
+      for(b=mesh_start;b<a;b++) {
+        CPrimitive *cprim;
+        cprim = I->Primitive+b;
+        sprintf(buffer,
+                "%6.4f %6.4f %6.4f,\n"
+                "%6.4f %6.4f %6.4f,\n"
+                "%6.4f %6.4f %6.4f,\n", 
+                cprim->c1[0],cprim->c1[1],cprim->c1[2],
+                cprim->c2[0],cprim->c2[1],cprim->c2[2],
+                cprim->c3[0],cprim->c3[1],cprim->c3[2]);
+        UtilConcatVLA(&vla,&cc,buffer);
+      }
+
+      /* close mesh */
+      UtilConcatVLA(&vla,&cc,
+                    "   ]\n"
+                    "  }\n"
+                    " }\n"
+                    "}\n");
+      mesh_obj=false;
+    }
   }
   
   *vla_ptr=vla;

Reply via email to