It is _very_ improbable that more than 128 partial normals contribute to
one vertex normal. That's why this was deliberately set to 128.

> Here is my code, also I have added some comments for my self to
> understand that is going on.
> But I still do not understand why "128"  possible normals used...
> (Code is modified to be compiled  with c++)
> (also fix added based on k < 128)
> // bSmooth -> smooth mode enabled 1
> //  if bSmooth == 0 => lib3ds_mesh_calculate_normals_ex goes faster with
> quality loose.
>
> void
> lib3ds_mesh_calculate_normals_ex(Lib3dsMesh *mesh, Lib3dsVector
> *normalL, int bSmooth)
> {
>   Lib3dsFaces **fl;
>   Lib3dsFaces *fa;
>   unsigned i,j,k;
>
>   if (!mesh->faces)
>   {
>     return;
>   }
>
>   fl=(Lib3dsFaces **)calloc(sizeof(Lib3dsFaces*),mesh->points);
>   ASSERT(fl);
>   fa=(Lib3dsFaces *)calloc(sizeof(Lib3dsFaces),3*mesh->faces);
>   ASSERT(fa);
>   k=0;
>   for (i=0; i<mesh->faces; ++i) {
>     Lib3dsFace *f=&mesh->faceL[i];
>     for (j=0; j<3; ++j) {
>       Lib3dsFaces* l=&fa[k++];
>       ASSERT(f->points[j]<mesh->points);
>       l->face=f;
>       l->next=fl[f->points[j]];
>       fl[f->points[j]]=l;
>     }
>   }
>
>   for (i=0; i<mesh->faces; ++i) // e.g. I have sometimes 20 000 faces...
>   { //(works to slow)
>     Lib3dsFace *f=&mesh->faceL[i];
>     Lib3dsVector n; // Can be declared here (change), posible move
> outside of for...
>
>
>         for (j=0; j<3; ++j) // face has 3 points (correct? )
>         {
>           // FIXME: static array needs at least check!!
>           Lib3dsVector N[128]; // why 128 ???
>           Lib3dsFaces *p;
>           int k,l;
>           int found;
>
>           // f->points[j] -> point index less then mesh->points
>           ASSERT(f->points[j]<mesh->points); // right
>           if ( f->smoothing && bSmooth)
>           {
>             lib3ds_vector_zero(n);
>             k=0; /// going throught every mesh face point
>             for (p=fl[f->points[j]]; p; p=p->next)
>             {
>               found=0;
>               for (l=0; l<k; ++l) // if any dot product  was found do
> search
>               { // of right normal in the array
>                  if( l >= 128 )
>                  {// if would be nice to have predefined TRACE macro
> instead of printf or sprintf
>                     TRACE("array N overflow: i=%d, j=%d, k=%d\n", i,j,k);
>                  }
>                     // |vec1*vect2| -> normal -> check length more than one?
>                  // why we skip normals less than one? answer:
>                  // yes -> because we are in the smooth mode
>                  if (fabs(lib3ds_vector_dot(N[l],
> p->face->normal)-1.0)<1e-5)
>                  {
>                     found=1;
>                     break;
>                  }
>               }
>
>               if (bSmooth && !found && k < 128) // k < 128 condition
> added (change)
>               {  // no dot product found k == 0 init face normal
>                 // removed condition "f->smoothing", already checked
> before...(change)
>                 if ( p->face->smoothing )
>                 {
>                   lib3ds_vector_add(n,n, p->face->normal);
>                   lib3ds_vector_copy(N[k], p->face->normal);
>                   ++k;
>                 }
>               }
>             }
>           }
>           else
>           {
>               lib3ds_vector_copy(n, f->normal);
>           }
>           lib3ds_vector_normalize(n);
>
>           lib3ds_vector_copy(normalL[3*i+j], n);
>         } // end for
>   }
>
>   free(fa);
>   free(fl);
> }
>
> Tomas
>
> Gernot Ziegler wrote:
> > Sorry, I can't run lib3ds due to the stated reasons.
> >
> > So the file works if you apply the fix that you proposed ?
> >
> > I am currently very busy, so my replies might take a while.
> >
> > /Gernot
> >
> >
> >
>

Servus,
  Gernot

-- 
T----------------------------W-E-L-C-O-M-E------------------------------T
O  The Austria <=> Sweden <=> Germany <=> Netherlands connection.....   H
|  http://www.mpi-sb.mpg.de/~gziegler | http://www.lysator.liu.se/~gz   E
\-----------------------------F-U-T-U-R-E-------------------------------/


_______________________________________________
lib3ds-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/lib3ds-devel

Reply via email to