bool Vertex::parallel(const Vertex& lhs, const Vertex& rhs) { //crossproduct of parallel vectors is (0, 0, 0) Vertex pvert = crossProduct(normalize(lhs), normalize(rhs)); return doubleeq(pvert.x(), 0.0) && doubleeq(pvert.y(), 0.0) && doubleeq(pvert.z(), 0.0); }
bool Vertex::operator<(const Vertex& other) const { if (!doubleeq(x(), other.x()) && x() < other.x()) return true; if (!doubleeq(y(), other.y()) && y() < other.y()) return true; if (!doubleeq(z(), other.z()) && z() < other.z()) return true; return false; }
char Plane::evaluate(const Vertex& point) const { double dist = Plane::dist(*this, point); if (doubleeq(dist, 0)) return 0; if (dist > 0) return 1; return -1; }
Vertex Vertex::normalize() const { double len = length(); if (doubleeq(len, 0.0)) return Vertex(*this); return Vertex( vertex_[0] / len, vertex_[1] / len, vertex_[2] / len); }
Vector Plane::intersectLine(const Plane& lhs, const Plane& rhs) { auto lhsEq = lhs.equation(); auto rhsEq = rhs.equation(); Vertex lhsNorm(lhsEq[0][0], lhsEq[0][1], lhsEq[0][2]); Vertex rhsNorm(rhsEq[0][0], rhsEq[0][1], rhsEq[0][2]); double lhsDist = lhsEq[0][3]; double rhsDist = rhsEq[0][3]; Vertex interLine = Vertex::crossProduct(lhsNorm, rhsNorm); Vertex commonPoint; if(!doubleeq(interLine.x(), 0)) { commonPoint.x(0); commonPoint.y((rhsNorm.z() * lhsDist - lhsNorm.z() * rhsDist) / interLine.x()); commonPoint.z((-rhsNorm.y() * lhsDist + lhsNorm.y() * rhsDist) / interLine.x()); } else if (!doubleeq(interLine.y(), 0)) { commonPoint.x((rhsNorm.z() * lhsDist - lhsNorm.z() * rhsDist) / interLine.y()); commonPoint.y(0); commonPoint.z((-rhsNorm.x() * lhsDist + lhsNorm.x() * rhsDist) / interLine.y()); } else if (!doubleeq(interLine.z(), 0)) { commonPoint.x((rhsNorm.y() * lhsDist - lhsNorm.y() * rhsDist) / interLine.z()); commonPoint.y((-rhsNorm.x() * lhsDist + lhsNorm.x() * rhsDist) / interLine.z()); commonPoint.z(0); } else { return Vector(); } Vector ret(interLine.normalize()); ret.beg(commonPoint); return ret; }
Vertex Vertex::normalize(const Vertex& v) { double len = v.length(); if (doubleeq(len, 0.0)) return v; return { v.x() / len, v.y() / len, v.z() / len }; }
Vertex Plane::intersectPoint(const Plane& p, const Vector& line) { Vertex normal = p.normal(); double num = -normal.dotProduct(line.beg() - p.p1); double denom = normal.dotProduct(line.vec()); if (doubleeq(denom, 0)) return Vertex(); double res = num / denom; return line.beg() + res * line.vec(); }
/* read the configuration file and the graph */ chaincolln chaincolln_readdata(void) { FILE *fileptr, *initzsfile; int i, j, k, ndom, nreln, d, r, nitem, dim, maxclass, initclass, relcl, ndim, domlabel, clusterflag, itemind, nchains, cind, zind; int *domlabels, *participants, participant; double val; double nig[DISTSIZE]; domain *doms; relation rn; int *initclasses, ***edgecounts, *relsizes; char prefix[MAXSTRING]; chaincolln cc; chain c, c0; #ifdef GSL gsl_rng *rng; const gsl_rng_type *T; gsl_permutation *perm ; size_t N; gsl_rng_env_setup(); T = gsl_rng_default; rng = gsl_rng_alloc(T); #endif fprintf(stdout,"A\n"); nchains = ps.nchains+1; nig[0] = ps.m; nig[1] = ps.v; nig[2] = ps.a; nig[3] = ps.b; fileptr = fopen(ps.configfile,"r"); if (fileptr == NULL) { fprintf(stderr, "couldn't read config file\n"); exit(1); } /* initial read of ps.configfile to get ps.maxdim, ps.maxrel, ps.maxitem, ps.maxclass */ fscanf(fileptr, "%s", prefix); fscanf(fileptr, "%d %d", &ndom, &nreln); relsizes= (int *) my_malloc(nreln*sizeof(int)); ps.maxrel = nreln; ps.maxitem = 0; ps.maxclass = 0; for (d = 0; d < ndom; d++) { fscanf(fileptr, "%d %d %d %d", &nitem, &maxclass, &initclass, &clusterflag); if (nitem > ps.maxitem) { ps.maxitem = nitem; } if (maxclass > ps.maxclass) { ps.maxclass= maxclass; } } fprintf(stdout,"B\n"); ps.maxdim = 0; for (r = 0; r < nreln; r++) { fscanf(fileptr, "%d", &ndim); relsizes[r] = ndim; if (ndim > ps.maxdim) { ps.maxdim = ndim; } for (dim=0; dim < ndim; dim++) { fscanf(fileptr, "%d", &domlabel); } } fclose(fileptr); fprintf(stdout,"C\n"); domlabels= (int *) my_malloc(ps.maxdim*sizeof(int)); participants= (int *) my_malloc(ps.maxdim*sizeof(int)); initclasses = (int *) my_malloc(ps.maxitem*sizeof(int)); fprintf(stdout,"D \n"); /* initial read of ps.graphname to get ps.maxobjtuples */ edgecounts = (int ***) my_malloc(ps.maxrel*sizeof(int **)); for (i = 0; i < ps.maxrel; i++) { edgecounts[i] = (int **) my_malloc(ps.maxdim*sizeof(int *)); for (j = 0; j < ps.maxdim; j++) { edgecounts[i][j] = (int *) my_malloc(ps.maxitem*sizeof(int)); for (k = 0; k < ps.maxitem; k++) { edgecounts[i][j][k] = 0; } } } ps.maxobjtuples = 0; fprintf(stdout,"D2 \n"); fileptr = fopen(ps.graphname,"r"); if (fileptr == NULL) { fprintf(stderr, "couldn't read graph\n"); exit(1); } while( fscanf( fileptr, " %d", &r)!=EOF ) { fprintf(stdout,"%s %d %d\n",__FILE__,__LINE__,r); ndim = relsizes[r]; fprintf(stdout,"%s %d %d\n",__FILE__,__LINE__,ndim); for (dim = 0; dim < ndim; dim++) { fscanf(fileptr, "%d", &participant); participants[dim] = participant; } fscanf(fileptr, "%lf", &val); for (dim = 0; dim < ndim; dim++) { fprintf(stdout,"D2 %d %d %d \n",r,dim,participants[dim]); edgecounts[r][dim][participants[dim]]++; fprintf(stdout,"D2 %d %d %d \n",r,dim,participants[dim]); } } fprintf(stdout,"E\n"); fclose(fileptr); for (i = 0; i < ps.maxrel; i++) { for (j = 0; j < ps.maxdim; j++) { for (k = 0; k < ps.maxitem; k++) { if (edgecounts[i][j][k] > ps.maxobjtuples) { ps.maxobjtuples = edgecounts[i][j][k]; } edgecounts[i][j][k]= 0; } } } fprintf(stdout,"F\n"); free(relsizes); for (i = 0; i < ps.maxrel; i++) { for (j = 0; j < ps.maxdim; j++) { free(edgecounts[i][j]); } free(edgecounts[i]); } free(edgecounts); fprintf(stdout,"G\n"); /* second read of ps.configfile where we set up datastructures */ fileptr = fopen(ps.configfile,"r"); if (ps.outsideinit) { initzsfile= fopen(ps.initfile,"r"); if (initzsfile == NULL) { fprintf(stderr, "couldn't read initzsfile\n"); exit(1); } } else { initzsfile = NULL; } fprintf(stdout,"H\n"); fscanf(fileptr, "%s", prefix); fscanf(fileptr, "%d %d", &ndom, &nreln); cc = chaincolln_create(nchains, ndom, nreln, prefix); c0 = chaincolln_getchain(cc, 0); fprintf(stdout,"I\n"); /* read domains */ /* input file: nitem maxclass initclass clusterflag*/ for (d = 0; d < ndom; d++) { fscanf(fileptr, "%d %d %d %d", &nitem, &maxclass, &initclass, &clusterflag); #ifdef GSL N = nitem; #endif if (ps.outsideinit) { for (zind = 0; zind < nitem; zind++) { fscanf(initzsfile, "%d", &initclasses[zind]); } } fprintf(stdout,"J\n"); /* add domains and items to chains */ for (cind = 0; cind < nchains; cind++) { c = chaincolln_getchain(cc, cind); chain_adddomain(c, d, nitem, maxclass, clusterflag, ps.alpha, ps.alphahyp, initclasses); #ifdef GSL perm = gsl_permutation_alloc(N); gsl_permutation_init(perm); gsl_ran_shuffle(rng, perm->data, N, sizeof(size_t)); #endif /* assign items to classes */ relcl = 0; for (i = 0; i < nitem; i++) { if (ps.outsideinit) { chain_additemtoclass(c, d, i, initclasses[i]); } else { if (relcl == initclass) relcl = 0; /* without the GNUSL, each chain gets initialized the same way. This * is suboptimal */ itemind = i; #ifdef GSL itemind = gsl_permutation_get(perm, i); #endif chain_additemtoclass(c, d, itemind, relcl); relcl++; } } #ifdef GSL gsl_permutation_free(perm); #endif } } #ifdef GSL gsl_rng_free(rng); #endif fprintf(stdout,"K\n"); /* read relations*/ /* input file: ndim d0 ... dn */ for (r = 0; r < nreln; r++) { fscanf(fileptr, "%d", &ndim); for (dim=0; dim < ndim; dim++) { fscanf(fileptr, "%d", &domlabel); domlabels[dim] = domlabel; } for (cind = 0; cind < nchains; cind++) { c = chaincolln_getchain(cc, cind); chain_addrelation(c, r, ndim, ps.betaprop, ps.betamag, nig, domlabels); } } if (ps.outsideinit) { fclose(initzsfile); } fprintf(stdout,"L\n"); fclose(fileptr); /* second read of ps.graphname: store edges*/ fileptr = fopen(ps.graphname,"r"); /* input file: relind p0 p1 p2 .. pn val */ while( fscanf( fileptr, " %d", &r)!= EOF ) { ndim = relation_getdim( chain_getrelation(c0, r) ); doms = relation_getdoms( chain_getrelation(c0, r) ); for (dim = 0; dim < ndim; dim++) { fscanf(fileptr, "%d", &participant); fprintf(stdout,"M %d %d\n",dim,participant); participants[dim] = participant; domlabels[dim] = domain_getlabel(doms[dim]); } for (i = 0; i < ndim; i++) { for (j = 0; j < i; j++) { if (participants[i] == participants[j] && domlabels[i] == domlabels[j]) { fprintf(stderr, "Self links not allowed.\n"); exit(1); } } } fscanf(fileptr, "%lf", &val); fprintf(stderr,"%d\n",nchains); for (cind = 0; cind < nchains; cind++) { c = chaincolln_getchain(cc, cind); chain_addedge(c, r, val, participants); rn = chain_getrelation(c, r); if (doubleeq(val, 0)) { relation_setmissing(rn, 1); } if (val > 1.5 && relation_getdtype(rn) != CONT) { relation_setdtype(rn, FREQ); } if (!doubleeq(val, (int) val)) { relation_setdtype(rn, CONT); relation_setmissing(rn, 1); /* XXX: no sparse continuous matrices */ } } } fprintf(stderr,"N\n"); fclose(fileptr); for (cind = 0; cind < nchains; cind++) { c = chaincolln_getchain(cc, cind); for (i = 0; i < chain_getndomains(c); i++) { chain_updatedomprobs(c, i); } } fprintf(stderr,"O\n"); free(domlabels); free(participants); free(initclasses); return cc; }
bool Vertex::equals(const Vertex& lhs, const Vertex& rhs) { return doubleeq(lhs.x(), rhs.x()) && doubleeq(lhs.y(), rhs.y()) && doubleeq(lhs.z(), rhs.z()); }
bool Vertex::operator==(const Vertex& other) const { return doubleeq(x(), other.x()) && doubleeq(y(), other.y()) && doubleeq(z(), other.z()); }