void tri_hp_ps::length() { int i,j,k,v0,v1,v2,indx,sind,tind,count; TinyVector<FLT,2> dx0,dx1,dx2,ep,dedpsi; FLT q,p,duv,um,vm,u,v; FLT sum,ruv,ratio; FLT length0,length1,length2,lengthept; FLT ang1,curved1,ang2,curved2; FLT norm; gbl->eanda = 0.0; for(tind=0;tind<ntri;++tind) { q = 0.0; p = 0.0; duv = 0.0; um = ug.v(tri(tind).pnt(2),0); vm = ug.v(tri(tind).pnt(2),1); for(j=0;j<3;++j) { v0 = tri(tind).pnt(j); p += fabs(ug.v(v0,2)); duv += fabs(u-um)+fabs(v-vm); } gbl->eanda(0) += 1./3.*(p*area(tind) +duv*gbl->mu*sqrt(area(tind)) ); gbl->eanda(1) += area(tind); } sim::blks.allreduce(gbl->eanda.data(),gbl->eanda_recv.data(),2,blocks::flt_msg,blocks::sum); norm = gbl->eanda_recv(0)/gbl->eanda_recv(1); gbl->fltwk(Range(0,npnt-1)) = 0.0; switch(basis::tri(log2p)->p()) { case(1): { for(i=0;i<nseg;++i) { v0 = seg(i).pnt(0); v1 = seg(i).pnt(1); ruv = gbl->mu/distance(v0,v1); sum = distance2(v0,v1)*(ruv*(fabs(ug.v(v0,0) -ug.v(v1,0)) +fabs(ug.v(v0,1) -ug.v(v1,1))) +fabs(ug.v(v0,2) -ug.v(v1,2))); gbl->fltwk(v0) += sum; gbl->fltwk(v1) += sum; } break; } default: { indx = basis::tri(log2p)->sm()-1; for(i=0;i<nseg;++i) { v0 = seg(i).pnt(0); v1 = seg(i).pnt(1); ruv = +gbl->mu/distance(v0,v1); sum = distance2(v0,v1)*(ruv*(fabs(ug.s(i,indx,0)) +fabs(ug.s(i,indx,1))) +fabs(ug.s(i,indx,2))); gbl->fltwk(v0) += sum; gbl->fltwk(v1) += sum; } /* BOUNDARY CURVATURE */ for(i=0;i<nebd;++i) { if (!(hp_ebdry(i)->is_curved())) continue; for(j=0;j<ebdry(i)->nseg;++j) { sind = ebdry(i)->seg(j); v1 = seg(sind).pnt(0); v2 = seg(sind).pnt(1); crdtocht1d(sind); /* FIND ANGLE BETWEEN LINEAR SIDES */ tind = seg(sind).tri(0); for(k=0;k<3;++k) if (tri(tind).seg(k) == sind) break; v0 = tri(tind).pnt(k); dx0(0) = pnts(v2)(0)-pnts(v1)(0); dx0(1) = pnts(v2)(1)-pnts(v1)(1); length0 = dx0(0)*dx0(0) +dx0(1)*dx0(1); dx1(0) = pnts(v0)(0)-pnts(v2)(0); dx1(1) = pnts(v0)(1)-pnts(v2)(1); length1 = dx1(0)*dx1(0) +dx1(1)*dx1(1); dx2(0) = pnts(v1)(0)-pnts(v0)(0); dx2(1) = pnts(v1)(1)-pnts(v0)(1); length2 = dx2(0)*dx2(0) +dx2(1)*dx2(1); basis::tri(log2p)->ptprobe1d(2,&ep(0),&dedpsi(0),-1.0,&cht(0,0),MXTM); lengthept = dedpsi(0)*dedpsi(0) +dedpsi(1)*dedpsi(1); ang1 = acos(-(dx0(0)*dx2(0) +dx0(1)*dx2(1))/sqrt(length0*length2)); curved1 = acos((dx0(0)*dedpsi(0) +dx0(1)*dedpsi(1))/sqrt(length0*lengthept)); basis::tri(log2p)->ptprobe1d(2,&ep(0),&dedpsi(0),1.0,&cht(0,0),MXTM); lengthept = dedpsi(0)*dedpsi(0) +dedpsi(1)*dedpsi(1); ang2 = acos(-(dx0(0)*dx1(0) +dx0(1)*dx1(1))/sqrt(length0*length1)); curved2 = acos((dx0(0)*dedpsi(0) +dx0(1)*dedpsi(1))/sqrt(length0*lengthept)); sum = gbl->curvature_sensitivity*(curved1/ang1 +curved2/ang2); gbl->fltwk(v0) += sum*gbl->error_target*norm*pnt(v0).nnbor; gbl->fltwk(v1) += sum*gbl->error_target*norm*pnt(v1).nnbor; } } break; } } for(i=0;i<npnt;++i) { gbl->fltwk(i) = pow(gbl->fltwk(i)/(norm*pnt(i).nnbor*gbl->error_target),1./(basis::tri(log2p)->p()+1+ND)); lngth(i) /= gbl->fltwk(i); } /* AVOID HIGH ASPECT RATIOS */ int nsweep = 0; do { count = 0; for(i=0;i<nseg;++i) { v0 = seg(i).pnt(0); v1 = seg(i).pnt(1); ratio = lngth(v1)/lngth(v0); if (ratio > 3.0) { lngth(v1) = 2.5*lngth(v0); ++count; } else if (ratio < 0.333) { lngth(v0) = 2.5*lngth(v1); ++count; } } ++nsweep; *gbl->log << "#aspect ratio fixes " << nsweep << ' ' << count << std::endl; } while(count > 0 && nsweep < 5); return; }
void ConvexHull2::addPoint(const Point2 &p) { if ( vertices.size() < 2 ) { if ( !vertices.contains( p ) ) { vertices.push_back( p ); } } else if ( vertices.size() == 2 ) { Segment2 seg( vertices[0], vertices[1] ); if ( seg.onLeft( p ) ) { vertices.push_back( p ); } else if ( seg.onRight( p ) ) { vertices.insert( 1, p ); } } else { // Find the first edge for which @p is on the positive side of the line defined by the edge int firstOnOrLeft = -1; for (int vertI = 0; vertI < vertices.size(); vertI++) { int vertJ = nextIndex( vertI, vertices.size() ); Segment2 seg( vertices[vertI], vertices[vertJ] ); if ( seg.onOrLeft( p ) ) { firstOnOrLeft = vertI; break; } } gs_assert( firstOnOrLeft != -1, "ConvexHull2::addPoint(): could not find first segment which has @p to the left\n" ); // Starting with at the edge after the first edge for which @p is on the positive side (found in previous step), // find the first edge for which @p is on the negative side int vertI = nextIndex( firstOnOrLeft, vertices.size() ); int firstOnRight = -1; for (int i = 0; i < vertices.size(); i++) { int vertJ = nextIndex( vertI, vertices.size() ); Segment2 seg( vertices[vertI], vertices[vertJ] ); if ( seg.onRight( p ) ) { firstOnRight = vertI; } vertI = vertJ; } // If @firstOnRight is -1, then @p is on the left of all edges, meaning that it is inside the hull; do nothing in this case if ( firstOnRight != -1 ) { // Starting the the first edge for which @p is on the negative side (found in previous step), look at the next // edge, and see if @p is on the negative side. If so, remove the vertex shared by both edges. int vertI = firstOnRight; int vertJ = nextIndex( vertI, vertices.size() ); int vertK = nextIndex( vertJ, vertices.size() ); Segment2 segB( vertices[vertJ], vertices[vertK] ); while ( segB.onRight( p ) ) { vertices.remove( vertJ ); if ( vertJ < vertI ) { vertI--; } vertJ = nextIndex( vertI, vertices.size() ); vertK = nextIndex( vertJ, vertices.size() ); segB = Segment2( vertices[vertJ], vertices[vertK] ); } // Insert @p after @vertI vertices.insert( vertI + 1, p ); } } }
/** TCP packets are inserted into streams. When the streams are closed * the contained data is returned as a pointer the data * it is up to the caller to free() the memory returned. */ unsigned char * assemble_tcp ( Payload &payload, in6addr_t *src_ip, in6addr_t *dst_ip, unsigned short src_port, unsigned short dst_port, unsigned int *rest, unsigned int seq, unsigned char *data, int len, char syn, char fin, char rst, char ack) { Stream_id id ( *src_ip, *dst_ip, src_port, dst_port ); Stream &str = g_tcp_streams[id]; bool data_avail = false; if (!str.has_content()) { Data_segment seg( data, len); str.add( syn,seq, seg); } else { if (rst == 1) { str.erase(); } else if (syn == 1) { str.erase(); Data_segment seg( data, len); str.add( syn,seq, seg); } else { Data_segment seg( data, len); str.add( syn,seq, seg); } } data = 0; if (str.has_content() ) { int size = str.get_size(); unsigned char *buffer = str.get_buffer(); int dns_size = (int(buffer[0])<<8) | buffer[1]; data_avail = (fin == 1) && (rst == 0); if (data_avail || dns_size+2==size) { *rest = size; if (*rest > 0xffff) *rest = 0xffff; data = (unsigned char*)payload.alloc(*rest); memcpy( data, buffer, *rest ); str.erase(); g_tcp_streams.erase(id); } } return data; }
int main(int argc, char **argv) { char line[MAX_LINE_CHARS]; int numTypes=0, numLines=0; int i,j,k; char *token; /* options */ for(i=1; i<argc; i++){ if(strcmp(argv[i], "-minSpan")==0){ minSpan = atoi(argv[++i]); }else if(strcmp(argv[i], "-maxSpan")==0){ maxSpan = atoi(argv[++i]); } } /* get the input */ scanf("%d %d\n", &numTypes, &numLines); numTokens = (int *)malloc(sizeof(int)*numLines); text = (int **)malloc(sizeof(int*)*numLines); i=-1; while(fgets(line, MAX_LINE_CHARS, stdin)!=NULL){ i++; token = strtok(line, " "); numTokens[i] = atoi(token); text[i] = (int *)malloc(sizeof(int)*numTokens[i]); j=-1; while((token = strtok(NULL, " ")) != NULL){ j++; text[i][j] = atoi(token); } } /* count occurrences */ cum = (int **)malloc(sizeof(int*)*(numLines+1)); total = (int *)malloc(sizeof(int)*(numLines+1)); for(i=0; i<numLines+1; i++){ cum[i] = (int *)malloc(sizeof(int)*numTypes); for(k=0; k<numTypes; k++){ cum[i][k] = 0; } total[i] = 0; } for(i=0; i<numLines; i++){ for(k=0; k<numTypes; k++){ cum[i+1][k] = cum[i][k]; total[i+1] += cum[i][k]; } for(j=0; j<numTokens[i]; j++){ cum[i+1][text[i][j]]++; } total[i+1] += numTokens[i]; } /* recursive segmentation */ printf("<seg start=\"%d\" end=\"%d\">\n", 0,numLines); edge = (double **)malloc(sizeof(double*)*numLines); for(i=0; i<numLines; i++){ edge[i] = (double *)malloc(sizeof(double)*(numLines+1)); } minCosts = (double *)malloc(sizeof(double)*(numLines+1)); counter = (int *)malloc(sizeof(int)*(numTypes)); bp = (int *)malloc(sizeof(int)*(numLines+1)); seg(0, numLines, numTypes, 1); printf("</seg>\n"); return 0; }
void seg(int minGap, int maxGap, int numTypes, int depth) { int numLines = maxGap - minGap; int i,j,start,end; int thisNumTypes=0,thisMinSpan,thisMaxSpan; int numGaps; int gaps[numLines+1]; double newCost; /* count thisNumTypes */ for(i=0; i<numTypes; i++){ counter[i]=0; } for(i=minGap; i<maxGap; i++){ for(j=0; j<numTokens[i]; j++){ counter[text[i][j]]++; } } for(i=0; i<numTypes; i++){ if(counter[i]>0){ thisNumTypes++; } } /* calculate thisMinSpan, thisMaxSpan */ thisMinSpan = (minSpan>0) ? MIN(minSpan,numLines) : 1; thisMaxSpan = (maxSpan>0) ? MIN(maxSpan,numLines) : numLines; /* edge */ for(i=minGap; i<maxGap; i++){ for(j=i+1; j<MIN(maxGap+1, i+thisMinSpan); j++){ edge[i-minGap][j-minGap] = LONG_MAX; } for(j=i+thisMinSpan; j<MIN(maxGap+1, i+thisMaxSpan+1); j++){ edge[i-minGap][j-minGap] = cost(i, j, numTypes, thisNumTypes, total[maxGap]-total[minGap]); } } /* initialization */ for(i=0; i<numLines+1; i++){ minCosts[i] = LONG_MAX; } minCosts[0] = 0; bp[0] = -1; /* forward */ for(start=minGap; start<maxGap; start++){ for(end=start+1; end<MIN(maxGap+1, start+thisMaxSpan+1); end++){ newCost = minCosts[start-minGap] + edge[start-minGap][end-minGap]; if(newCost < minCosts[end-minGap]){ minCosts[end-minGap] = newCost; bp[end-minGap] = start; } } } /* backward */ i=maxGap; numGaps=0; while(i>=0){ gaps[numGaps++] = i; i=bp[i-minGap]; } if(numGaps<=2){ return; } for(i=1; i<numGaps; i++){ for(j=0;j<depth;j++)printf("\t"); printf("<seg start=\"%d\" end=\"%d\">\n", gaps[numGaps-i],gaps[numGaps-i-1]); seg(gaps[numGaps-i], gaps[numGaps-i-1], numTypes, depth+1); for(j=0;j<depth;j++)printf("\t"); printf("</seg>\n"); } }
void DistanceRayToSegment(const Ray& ray, const LineSeg& segment, float* out_distTo, float* out_distBetween, float3* out_pos, float3* out_nor) { // Find closest distance between ray and segment: // Construct seg ray: // seg.direction = normal(segment.B - segment.A) // seg.pos = segment.A // // Construct plane through ray.pos that contains both ray.direction and segment.direction // Construct second plane through segment.pos that contains both ray.direction and segment.direction // The normal of both these planes will be: // planeNormal = normal(cross(ray.direction, seg.direction)) // planeR . planeNormal = ray.pos . planeNormal // planeS . planeNormal = seg.pos . planeNormal // // The distance between these planes will be ray-to-seg projected on the planeNormal: // distRayPosToSegment = (seg.pos - ray.pos) . planeNormal // // Now we need to find where this closest point occurs to return distanceToClosest and check if // it actually occurs between segment.A and segment.B: // rayLine = ray.pos + tRay * ray.direction // segLine = seg.pos + tSeg * seg.direction // // Since we already have the normal and distance between ray and segment: // rayLine + distBetweenRaySegment * planeNormal = segLine // // By setting components equal we can solve for tRay and tSeg: // (B * seg.direction.x + A * seg.direction.y) // tRay = --------------------------------------------------------------------------- // (ray.direction.y * seg.direction.x - ray.direction.x * seg.direction.y) // // where // A = ray.pos.x - seg.pos.x + distBetweenRaySegment * planeNormal.x // B = seg.pos.y - ray.pos.y - distBetweenRaySegment * planeNormal.y // // tSeg = (A + tRay * ray.direction.x) / seg.direction.x // // if tSeg < 0 => use point-to-ray-distance(segment.A, ray) // else if tSeg > length(segment.B - segment.A) => use point-to-ray-distance(segment.B, ray) // else => distToClosest = tRay // posClosest = tRay * ray.direction + ray.position // Ray seg(segment.A, segment.B - segment.A); // check for parallel seg and ray float3 planeNormal = normalize(cross(ray.direction, seg.direction)); float distBetweenRaySegment = dot(seg.pos - ray.pos, planeNormal); float A = ray.pos.x - seg.pos.x + distBetweenRaySegment * planeNormal.x; float B = seg.pos.y - ray.pos.y - distBetweenRaySegment * planeNormal.y; float tRay = (B * seg.direction.x + A * seg.direction.y) / (ray.direction.y * seg.direction.x - ray.direction.x * seg.direction.y); float tSeg = (A + tRay * ray.direction.x) / seg.direction.x; float segmentLength = length(segment.B - segment.A); if (tRay < 0) // the segment is behind us { DistancePointToSegment(segment, ray.pos, out_distTo, out_distBetween, out_pos, out_nor); } else if (tSeg < 0) // closest is before segment.A { DistanceRayToPoint(ray, segment.A, out_distTo, out_distBetween, out_pos, out_nor); } else if (tSeg > segmentLength) // closest is after segment.B { DistanceRayToPoint(ray, segment.B, out_distTo, out_distBetween, out_pos, out_nor); } else { *out_distTo = tRay; *out_distBetween = fabsf(distBetweenRaySegment); *out_pos = ray.pos + tRay * ray.direction; *out_nor = planeNormal; } }
void doinit(SYM *sp) { static bool first = true; char lbl[200]; int algn; enum e_sg oseg; char buf[500]; std::streampos endpoint; TYP *tp; hasPointer = false; if (first) { firstPrim = true; first = false; } oseg = noseg; lbl[0] = 0; // Initialize constants into read-only data segment. Constants may be placed // in ROM along with code. if (sp->isConst) { oseg = rodataseg; } if (sp->storage_class == sc_thread) { if (sp->tp->type==bt_struct || sp->tp->type==bt_union) algn = imax(sp->tp->alignment,8); else if (sp->tp->type==bt_pointer)// && sp->tp->val_flag) algn = imax(sp->tp->GetBtp()->alignment,8); else algn = 2; seg(oseg==noseg ? tlsseg : oseg,algn); nl(); } else if (sp->storage_class == sc_static || lastst==assign) { if (sp->tp->type==bt_struct || sp->tp->type==bt_union) algn = imax(sp->tp->alignment,8); else if (sp->tp->type==bt_pointer)// && sp->tp->val_flag) algn = imax(sp->tp->GetBtp()->alignment,8); else algn = 2; seg(oseg==noseg ? dataseg : oseg,algn); /* initialize into data segment */ nl(); /* start a new line in object */ } else { if (sp->tp->type==bt_struct || sp->tp->type==bt_union) algn = imax(sp->tp->alignment,8); else if (sp->tp->type==bt_pointer)// && sp->tp->val_flag) algn = imax(sp->tp->GetBtp()->alignment,8); else algn = 2; seg(oseg==noseg ? (lastst==assign ? dataseg : bssseg) : oseg,algn); /* initialize into data segment */ nl(); /* start a new line in object */ } if (sp->storage_class == sc_static || sp->storage_class == sc_thread) { //strcpy_s(glbl, sizeof(glbl), gen_label((int)sp->value.i, (char *)sp->name->c_str(), GetNamespace(), 'D')); if (sp->tp->IsSkippable()) { patchpoint = ofs.tellp(); sprintf_s(buf, sizeof(buf), "\talign\t8\n\tdw\t$FFF0200000000001\n"); ofs.printf(buf); } sp->realname = my_strdup(put_label((int)sp->value.i, (char *)sp->name->c_str(), GetNamespace(), 'D')); strcpy_s(glbl2, sizeof(glbl2), gen_label((int)sp->value.i, (char *)sp->name->c_str(), GetNamespace(), 'D')); } else { if (sp->storage_class == sc_global) { strcpy_s(lbl, sizeof(lbl), "public "); if (curseg==dataseg) strcat_s(lbl, sizeof(lbl), "data "); else if (curseg==bssseg) strcat_s(lbl, sizeof(lbl), "bss "); else if (curseg==tlsseg) strcat_s(lbl, sizeof(lbl), "tls "); } strcat_s(lbl, sizeof(lbl), sp->name->c_str()); if (sp->tp->IsSkippable()) { patchpoint = ofs.tellp(); sprintf_s(buf, sizeof(buf), "\talign\t8\n\tdw\t$FFF0200000000001\n"); ofs.printf(buf); } strcpy_s(glbl2, sizeof(glbl2), sp->name->c_str()); gen_strlab(lbl); } if (lastst == kw_firstcall) { GenerateByte(1); return; } else if( lastst != assign) { hasPointer = sp->tp->FindPointer(); genstorage(sp->tp->size); } else { NextToken(); hasPointer = sp->tp->FindPointer(); typ_sp = 0; tp = sp->tp; push_typ(tp); while (tp = tp->GetBtp()) { push_typ(tp); } brace_level = 0; sp->tp->Initialize(nullptr); if (sp->tp->numele == 0) { if (sp->tp->GetBtp()) { if (sp->tp->GetBtp()->type == bt_char || sp->tp->GetBtp()->type == bt_uchar || sp->tp->GetBtp()->type == bt_ichar || sp->tp->GetBtp()->type == bt_iuchar ) { sp->tp->numele = laststrlen; sp->tp->size = laststrlen; } } } } if (!hasPointer && sp->tp->IsSkippable()) { endpoint = ofs.tellp(); ofs.seekp(patchpoint); sprintf_s(buf, sizeof(buf), "\talign\t8\n\tdw\t$%I64X\n", ((genst_cumulative + 7LL) >> 3LL) | 0xFFF0200000000000LL); ofs.printf(buf); ofs.seekp(endpoint); genst_cumulative = 0; }