// these are a triangle face ccw when looking from outside the polyhedron PolyTriangle::PolyTriangle(Vector vp0,Vector vp1,Vector vp2) { v0=vp0; v1=vp1; v2=vp2; // normal is (v1-v0) X (v2-v0) Vector a,b; SubVector(CopyVector(&a,&v1),&v0); SubVector(CopyVector(&b,&v2),&v0); CrossProduct(&n,&a,&b); if(!DbleEqual(n.x,0)) { m11 = b.z/n.x; m12 = -b.y/n.x; m21 = -a.z/n.x; m22 = a.y/n.x; style = USE_NX; } else if(!DbleEqual(n.z,0)) { m11 = b.y/n.z; m12 = -b.x/n.z; m21 = -a.y/n.z; m22 = a.x/n.z; style = USE_NZ; } else { m11 = -b.z/n.y; m12 = b.x/n.y; m21 = a.z/n.y; m22 = -a.x/n.y; style = USE_NY; } fmin.x = fmin(fmin(v0.x,v1.x),v2.x); fmin.y = fmin(fmin(v0.y,v1.y),v2.y); fmin.z = fmin(fmin(v0.z,v1.z),v2.z); fmax.x = fmax(fmax(v0.x,v1.x),v2.x); fmax.y = fmax(fmax(v0.y,v1.y),v2.y); fmax.z = fmax(fmax(v0.z,v1.z),v2.z); }
void CLoad3DS::ComputeNormals(t3DModel *pModel) { int i; CVector3 vVector1, vVector2, vNormal, vPoly[3]; if(pModel->numOfObjects <= 0) return; for(int index = 0; index < pModel->numOfObjects; index++) { t3DObject *pObject = &(pModel->pObject[index]); pObject->pNormals = new CVector3 [pObject->numOfVerts]; for(i=0; i < pObject->numOfFaces; i++) { unsigned short* pIdx = pObject->pFaces[i].vertIndex; vPoly[0] = pObject->pVerts[pIdx[0]]; vPoly[1] = pObject->pVerts[pIdx[1]]; vPoly[2] = pObject->pVerts[pIdx[2]]; vVector1 = SubVector(vPoly[0], vPoly[1]); vVector2 = SubVector(vPoly[0], vPoly[2]); vNormal = Cross(vVector1, vVector2); Normalize(vNormal); AddToVector(pObject->pNormals[pIdx[0]], vNormal); AddToVector(pObject->pNormals[pIdx[1]], vNormal); AddToVector(pObject->pNormals[pIdx[2]], vNormal); } for (i = 0; i < pObject->numOfVerts; i++) { Normalize(pObject->pNormals[i]); } } }
// Assumes that there is either no exogenous variables or there is a single // exogenous variable that is constant and equal to one. The unconditional // mean is obtained from the reduced form companion matrix. TDenseVector UnconditionalMean(const TDenseMatrix &A0, const TDenseMatrix &Aplus, bool IsConstant) { int n_lags=NumberLags(A0,Aplus,IsConstant), n_vars=A0.cols; if (!IsConstant) return TDenseVector(n_vars,0.0); TDenseMatrix B=ReducedForm(A0,Aplus); if (n_lags == 0) return ColumnVector(B,0); TDenseMatrix C=CompanionMatrix(B,n_lags); TDenseVector b(n_vars*n_lags,0.0); b.InsertColumnVector(0,B,n_vars*n_lags,0,n_vars-1); try { return SubVector(InverseMultiply(Identity(n_vars*n_lags) - C,b),0,n_vars-1); } catch (dw_exception &e) { throw dw_exception("UnconditionalMean(): Unconditional mean does not exist"); } }
/* Returns the properly scalled log density: log(p(b(i) | b(i+1), ... b(n), Y)) This is computed via Gibbs simulation using Chib's method, since p(b(i) | b(1), ... b(i-1), b(i+1), ... b(n), Y) is known. */ double SBVAR_symmetric_linear::LogConditionalA0_gibbs(const TDenseVector &p, int i, int ndraws, int thin, int burn_in) { TDenseVector b=SubVector(p,begin_b[i],begin_b[i]+dim_b[i]-1); SetParameters(p.vector); double constant=0.5*(lambda_T+1)*log(0.5*lambda_T) + 0.5*(dim_b[i]-1)*log(lambda_T*0.159154943091895) - dw_log_gamma(0.5*(lambda_T+1)) - LogAbsDeterminant(Simulate_SqrtS[i]); // 0.159154943091895 = 1/(2*pi) if (i == n_vars-1) return constant + LogConditionalA0_kernel(A0,b,i); else { for (int k=burn_in; k > 0; k--) SimulateA0(i); double sum=-1.0e300; for (int k=ndraws; k > 0; k--) { for (int j=thin; j > 0; j--) SimulateA0(i); sum=AddLogs(sum,LogConditionalA0_kernel(A0,b,i)); } return constant + sum - log((double)ndraws); } }
/* Compute form-factors from the shooting patch to every elements */ static void ComputeFormfactors(unsigned long shootPatch) { unsigned long i; TVector3f up[5]; TPoint3f lookat[5]; TPoint3f center; TVector3f normal, tangentU, tangentV, vec; int face; double norm; TPatch* sp; double* fp; TElement* ep; /* get the center of shootPatch */ sp = &(params->patches[shootPatch]); center = sp->center; normal = sp->normal; /* rotate the hemi-cube along the normal axis of the patch randomly */ /* this will reduce the hemi-cube aliasing artifacts */ do { vec.x = RandomFloat; vec.y = RandomFloat; vec.z = RandomFloat; /* get a tangent vector */ CrossVector(tangentU, normal, vec); NormalizeVector(norm, tangentU); } while (norm==0); /* bad choice of the random vector */ /* compute tangentV */ CrossVector(tangentV, normal, tangentU); /* assign the lookats and ups for each hemicube face */ AddVector(lookat[0], center, normal); up[0] = tangentU; AddVector(lookat[1], center, tangentU); up[1] = normal; AddVector(lookat[2], center, tangentV); up[2] = normal; SubVector(lookat[3], center, tangentU); up[3] = normal; SubVector(lookat[4], center, tangentV); up[4] = normal; /* position the hemicube slightly above the center of the shooting patch */ ScaleVector(normal, params->worldSize*0.0001); AddVector(hemicube.view.camera, center, normal); /* clear the formfactors */ fp = formfactors; for (i=params->nElements; i--; fp++) *fp = 0.0; for (face=0; face < 5; face++) { hemicube.view.lookat = lookat[face]; hemicube.view.up = up[face]; /* draw elements */ BeginDraw(&(hemicube.view), kBackgroundItem); for (i=0; i< params->nElements; i++) DrawElement(¶ms->elements[i], i); /* color element i with its index */ EndDraw(); /* get formfactors */ if (face==0) SumFactors(formfactors, hemicube.view.xRes, hemicube.view.yRes, hemicube.view.buffer, hemicube.topFactors); else SumFactors(formfactors, hemicube.view.xRes, hemicube.view.yRes/2, hemicube.view.buffer, hemicube.sideFactors); } /* compute reciprocal form-factors */ ep = params->elements; fp = formfactors; for (i=params->nElements; i--; ep++, fp++) { *fp *= sp->area / ep->area; /* This is a potential source of hemi-cube aliasing */ /* To do this right, we need to subdivide the shooting patch and reshoot. For now we just clip it to unity */ if ((*fp) > 1.0) *fp = 1.0; } }
/* Compute form-factors from the shooting patch to every elements */ static void ComputeFormfactors(unsigned long shootPatch) { unsigned long i; TVector3f up[5]; TPoint3f lookat[5]; TPoint3f center; TVector3f normal, tangentU, tangentV, vec; int face; double norm; TPatch* sp; double* fp; TElement* ep; double plane_eq[4]; /* get the center of shootPatch */ sp = &(params->patches[shootPatch]); center = sp->center; normal = sp->normal; plane_eq[0] = (double)normal.x; plane_eq[1] = (double)normal.y; plane_eq[2] = (double)normal.z; plane_eq[3] = (double)-(normal.x*center.x + normal.y*center.y + normal.z*center.z); /* rotate the hemi-cube along the normal axis of the patch randomly */ /* this will reduce the hemi-cube aliasing artifacts */ do { vec.x = RandomFloat; vec.y = RandomFloat; vec.z = RandomFloat; /* get a tangent vector */ CrossVector(tangentU, normal, vec); NormalizeVector(norm, tangentU); } while (norm==0); /* bad choice of the random vector */ /* compute tangentV */ CrossVector(tangentV, normal, tangentU); /* assign the lookats and ups for each hemicube face */ AddVector(lookat[0], center, normal); up[0] = tangentU; AddVector(lookat[1], center, tangentU); up[1] = normal; AddVector(lookat[2], center, tangentV); up[2] = normal; SubVector(lookat[3], center, tangentU); up[3] = normal; SubVector(lookat[4], center, tangentV); up[4] = normal; /* position the hemicube slightly above the center of the shooting patch */ ScaleVector(normal, params->worldSize*0.00000001); AddVector(hemicube.view.camera, center, normal); /* clear the formfactors */ fp = formfactors; for (i=params->nElements; i--; fp++) *fp = 0.0; for (face=0; face < 5; face++) { hemicube.view.lookat = lookat[face]; hemicube.view.up = up[face]; /* draw elements */ if (bits_for_RGB == 24) { /* a 24-bit display */ BeginHCDraw(&(hemicube.view), kBackgroundItem, plane_eq); for (i=0; i< params->nElements; i++) DrawHCElement(¶ms->elements[i], i); /* color element i with its index */ EndHCDraw(&(hemicube.view)); } else if (bits_for_RGB == 8) { /* an 8-bit display */ /* this is a quick hack to make it work for 8-bit displays maybe a better way could be found ??? */ part_of_id = 1; /* processing first half of polygon ids */ BeginHCDraw(&(hemicube.view), 255, plane_eq); for (i=0; i< params->nElements; i++) DrawHCElement(¶ms->elements[i], i); /* color element i with its index */ EndHCDraw(&(hemicube.view)); part_of_id = 2; /* second half of polygon ids */ BeginHCDraw(&(hemicube.view), 255, plane_eq); for (i=0; i< params->nElements; i++) DrawHCElement(¶ms->elements[i], i); /* color element i with its index */ EndHCDraw(&(hemicube.view)); } else { printf("Unexpected bits per RGB colour, exiting"); //exit(0); } /* get formfactors */ if (face==0) SumFactors(formfactors, hemicube.view.xRes, hemicube.view.yRes, hemicube.view.buffer, hemicube.topFactors,0); else SumFactors(formfactors, hemicube.view.xRes, hemicube.view.yRes, hemicube.view.buffer, hemicube.sideFactors, hemicube.view.yRes/2); } /* compute reciprocal form-factors */ ep = params->elements; fp = formfactors; for (i=params->nElements; i--; ep++, fp++) { *fp *= sp->area / ep->area; /* This is a potential source of hemi-cube aliasing */ /* To do this right, we need to subdivide the shooting patch and reshoot. For now we just clip it to unity */ if ((*fp) > 1.0) *fp = 1.0; } }