void calcnormals(Gobject *o, int mallocnormals){ int aktpoly = -1, edges,i1,i2,i3, normanz = 0, aktnorm = 0; vec norm; double erg; while ((edges = o->polygons[++aktpoly])!=0){ normanz++; aktpoly+=edges+2; } /* printf("Calcnormals.Normanzahl:%i\n",normanz);*/ if (mallocnormals) o->na=malloc(sizeof(vec)*normanz); aktpoly=0; for (aktnorm=0;aktnorm<normanz;aktnorm++){ i1=o->polygons[aktpoly+3]; i2=o->polygons[aktpoly+4]; i3=o->polygons[aktpoly+5]; veq(norm,vnorm(vp(vdiff(o->va[i2],o->va[i1]),vdiff(o->va[i3],o->va[i1])))); if (o->type==V3DPOLYHEDRON){ erg=sp(vdiff(o->va[i1],o->posmc),norm);vreset(); if (erg<0) veq(norm,sm(-1,norm)); if (erg==0){printf("Fehler in calcnormals: Normale nicht definiert\n"); } } /* pv(norm);*/ veq(o->na[aktnorm],norm); o->polygons[aktpoly+1]=aktnorm; aktpoly+=o->polygons[aktpoly]+3; } }
/* apply the constraints with SHAKE and RATTTLE */ static void constrain(int itmax, double tol) { int i, it; double vl, dev, maxdev, dot; double dx0[N][D], dx[D], dv[D]; for ( i = 1; i < N; i++ ) { vdiff(dx0[i], x[i], x[0]); } /* SHAKE */ for ( it = 0; it < itmax; it++ ) { maxdev = 0; for ( i = 1; i < N; i++ ) { /* constraint the distance between atoms 0 and i */ vdiff(dx, x[i], x[0]); dev = vsqr(dx) - 1; if ( dev > maxdev ) maxdev = dev; dot = vdot(dx, dx0[i]); if ( dot < 0.2 ) { /* really bad */ dot = 0.2; } dev = dev / (2 * dot); vsinc(x[i], dx0[i], -dev * 0.5); vsinc(x[0], dx0[i], dev * 0.5); } if ( maxdev < tol ) break; } /* RATTLE */ for ( it = 0; it < itmax; it++ ) { maxdev = 0; for ( i = 1; i < N; i++ ) { /* annihilate the parallel velocity */ vdiff(dx, x[i], x[0]); vdiff(dv, v[i], v[0]); /* the relative velocity should be perpendicular * to dr, if not, subtract the nonperpendicular one */ vl = vdot(dv, dx); /* because the reference length is 1.0 * we do not have to divide it */ vsinc(v[i], dx, -vl * 0.5); vsinc(v[0], dx, +vl * 0.5); dev = fabs(vl); if ( dev > maxdev ) { maxdev = dev; } } if ( maxdev < tol ) break; } }
void flaeche_3d(int c,vecp rp, vecp v1p,vecp v2p,int n1, int n2){ int i,j; vec r,r2,v1,v2; veq(r,mc2wc(rp)); veq(v1,sm(1.0/n1,vdiff(mc2wc(vsum(rp,v1p)),r))); veq(v2,sm(1.0/n2,vdiff(mc2wc(vsum(rp,v2p)),r))); /* veq(v1,mc2wcnorm(v1p))); veq(v2,sm(1.0/n2,mc2wcnorm(v2p)));*/ veq(r2,vsum(r,sm(0.5,vsum(v1,v2)))); for (i=0;i<n1;i++) for (j=0;j<n2;j++) einfache_flaeche_wc_3d(c, vsum(r2,vsum(sm(1.0*i,v1),sm(1.0*j,v2))), vsum(r,vsum(sm(1.0*i,v1),sm(1.0*j,v2))), vsum(r,vsum(sm(1.0*(i+1),v1),sm(1.0*j,v2))), vsum(r,vsum(sm(1.0*(i+1),v1),sm(1.0*(j+1),v2))), vsum(r,vsum(sm(1.0*i,v1),sm(1.0*(j+1),v2)))); }
/* bond energy 1/2 k (r - r0)^2 */ static double potbond(double *a, double *b, double r0, double kr, double *fa, double *fb) { double dx[D], r, dr, amp; r = vnorm( vdiff(dx, a, b) ); dr = r - r0; amp = kr * dr / r; vsinc(fa, dx, -amp); vsinc(fb, dx, amp); return 0.5 * kr * dr * dr; }
void contour3d(Contour* c){ int i,j,*k; vecp v; matp m; if (c->initialized==0){ c->tva=malloc((c->vaakt+1)*sizeof(vec)); c->na=malloc((c->polakt+1)*sizeof(vec)); c->sp=malloc((c->polakt+1)*sizeof(vec)); c->points=malloc((c->polakt+1)*sizeof(DPoint)); c->Garray=malloc((c->polakt+1)*sizeof(Gobject)); for (i=0;i<=c->polakt;i++) { c->Garray[i].na=c->na; c->Garray[i].tva=c->tva; c->Garray[i].poswc=(vecp) &c->sp[i]; /* compiler distinguishes between vec* and vecp */ c->Garray[i].points=c->points; c->Garray[i].polygons=&c->pol[i*6]; c->Garray[i].drawstruct=drawtrianglelight; j=c->pol[i*6]; veq(c->na[i],vnorm(vp(vdiff(c->va[j+4],c->va[j+3]),vdiff(c->va[j+5],c->va[j+3])))); } c->initialized=1; } /* Transformation, die jedes Mal durchlaufen wird. */ meq(c->mc2wcmn,getmc2wcmn()); meq(c->mc2wcm,getmc2wcm()); veq(c->mc2wcv,getmc2wcv()); m=c->mc2wcm; v=c->mc2wcv; for (i=0;i<=c->vaakt;i++) { veq(c->tva[i],vsum(matvecmul(m,c->va[i]),v)); wc2dc(&c->points[i].x,&c->points[i].y,c->tva[i]); } for (i=0;i<=c->polakt;i++){ /* Schwerpunkt in Weltkoordinaten */ k=&c->pol[i*6+3]; veq(c->sp[i],sm(0.33333, vsum(vsum(c->tva[*k],c->tva[*(k+1)]),c->tva[*(k+2)]))); insertGobject(&c->Garray[i]); } }
void KDTree::n_nearest(std::vector<float>& qv, int nn, KDTreeResultVector& result) { SearchRecord sr(qv, *this, result); std::vector<float> vdiff(dim, 0.0); result.clear(); sr.centeridx = -1; sr.correltime = 0; sr.nn = nn; root->search(sr); if (sort_results) sort(result.begin(), result.end()); }
void KDTree::r_nearest(std::vector<float>& qv, float r2, KDTreeResultVector& result) { // search for all within a ball of a certain radius SearchRecord sr(qv, *this, result); std::vector<float> vdiff(dim, 0.0); result.clear(); sr.centeridx = -1; sr.correltime = 0; sr.nn = 0; sr.ballsize = r2; root->search(sr); if (sort_results) sort(result.begin(), result.end()); }
void DoFlares(GLfloat from[3], GLfloat at[3], GLfloat light[3], GLfloat near_clip) { GLfloat view_dir[3], tmp[3], light_dir[3], position[3], dx[3], dy[3], center[3], axis[3], sx[3], sy[3], dot, global_scale = 1.5; GLuint bound_to = 0; int i; /* view_dir = normalize(at-from) */ vdiff(view_dir, at, from); vnorm(view_dir); /* center = from + near_clip * view_dir */ vscale(tmp, view_dir, near_clip); vadd(center, from, tmp); /* light_dir = normalize(light-from) */ vdiff(light_dir, light, from); vnorm(light_dir); /* light = from + dot(light,view_dir)*near_clip*light_dir */ dot = vdot(light_dir, view_dir); vscale(tmp, light_dir, near_clip / dot); vadd(light, from, light_dir); /* axis = light - center */ vdiff(axis, light, center); vcopy(dx, axis); /* dx = normalize(axis) */ vnorm(dx); /* dy = cross(dx,view_dir) */ vcross(dy, dx, view_dir); glDisable(GL_DEPTH_TEST); glDisable(GL_DITHER); glEnable(GL_BLEND); glBlendFunc(GL_ONE, GL_ONE); for (i = 0; i < num_flares; i++) { vscale(sx, dx, flare[i].scale * global_scale); vscale(sy, dy, flare[i].scale * global_scale); glColor3fv(flare[i].color); /* Note logic below to eliminate duplicate texture binds. */ if (flare[i].type < 0) { if (bound_to) glEnd(); glBindTexture(GL_TEXTURE_2D, shineTex[shine_tic]); bound_to = shineTex[shine_tic]; shine_tic = (shine_tic + 1) % 10; glBegin(GL_QUADS); } else { if (bound_to != flareTex[flare[i].type]) { glEnd(); glBindTexture(GL_TEXTURE_2D, flareTex[flare[i].type]); bound_to = flareTex[flare[i].type]; glBegin(GL_QUADS); } } /* position = center + flare[i].loc * axis */ vscale(tmp, axis, flare[i].loc); vadd(position, center, tmp); glTexCoord2f(0.0, 0.0); vadd(tmp, position, sx); vadd(tmp, tmp, sy); glVertex3fv(tmp); glTexCoord2f(1.0, 0.0); vdiff(tmp, position, sx); vadd(tmp, tmp, sy); glVertex3fv(tmp); glTexCoord2f(1.0, 1.0); vdiff(tmp, position, sx); vdiff(tmp, tmp, sy); glVertex3fv(tmp); glTexCoord2f(0.0, 1.0); vadd(tmp, position, sx); vdiff(tmp, tmp, sy); glVertex3fv(tmp); } glEnd(); }