static int reallyroutespline (Pedge_t *edges, int edgen, Ppoint_t *inps, int inpn, Ppoint_t ev0, Ppoint_t ev1) { Ppoint_t p1, p2, cp1, cp2, p; Pvector_t v1, v2, splitv, splitv1, splitv2; double maxd, d, t; int maxi, i, spliti; static tna_t *tnas; static int tnan; if (tnan < inpn) { if (!tnas) { if (!(tnas = (tna_t *)malloc (sizeof (tna_t) * inpn))) return -1; } else { if (!(tnas = (tna_t *)realloc (tnas, sizeof (tna_t) * inpn))) return -1; } tnan = inpn; } tnas[0].t = 0; for (i = 1; i < inpn; i++) tnas[i].t = tnas[i - 1].t + dist (inps[i], inps[i - 1]); for (i = 1; i < inpn; i++) tnas[i].t /= tnas[inpn - 1].t; for (i = 0; i < inpn; i++) { tnas[i].a[0] = scale (ev0, B1 (tnas[i].t)); tnas[i].a[1] = scale (ev1, B2 (tnas[i].t)); } if (mkspline (inps, inpn, tnas, ev0, ev1, &p1, &v1, &p2, &v2) == -1) return -1; if (splinefits (edges, edgen, p1, v1, p2, v2, (inpn == 2 ? 1 : 0))) return 0; cp1 = add (p1, scale (v1, 1 / 3.0)); cp2 = sub (p2, scale (v2, 1 / 3.0)); for (maxd = -1, maxi = -1, i = 1; i < inpn - 1; i++) { t = tnas[i].t; p.x = B0 (t) * p1.x + B1 (t) * cp1.x + B2 (t) * cp2.x + B3 (t) * p2.x; p.y = B0 (t) * p1.y + B1 (t) * cp1.y + B2 (t) * cp2.y + B3 (t) * p2.y; if ((d = dist (p, inps[i])) > maxd) maxd = d, maxi = i; } spliti = maxi; splitv1 = normv (sub (inps[spliti], inps[spliti - 1])); splitv2 = normv (sub (inps[spliti + 1], inps[spliti])); splitv = normv (add (splitv1, splitv2)); reallyroutespline (edges, edgen, inps, spliti + 1, ev0, splitv); reallyroutespline (edges, edgen, &inps[spliti], inpn - spliti, splitv, ev1); return 0; }
void toast::qarray::exp ( size_t n, double const * q_in, double * q_out ) { toast::mem::simd_array<double> normv(n); toast::qarray::amplitude ( n, 4, 3, q_in, normv ); double exp_q_w; #pragma omp parallel for default(shared) private(exp_q_w) schedule(static) for ( size_t i = 0; i < n; ++i ) { exp_q_w = ::exp ( q_in[4*i + 3] ); q_out[4*i + 3] = exp_q_w * ::cos ( normv[i] ); exp_q_w /= normv[i]; exp_q_w *= ::sin ( normv[i] ); for ( size_t j = 0; j < 3; ++j ) { q_out[4*i + j] = exp_q_w * q_in[4*i + j]; } } return; }
int Proutespline (Pedge_t *edges, int edgen, Ppolyline_t input, Ppoint_t *evs, Ppolyline_t *output) { #if 0 Ppoint_t p0, p1, p2, p3; Ppoint_t *pp; Pvector_t v1, v2, v12, v23; int ipi, opi; int ei, p2ei; Pedge_t *e0p, *e1p; #endif Ppoint_t *inps; int inpn; /* unpack into previous format rather than modify legacy code */ inps = input.ps; inpn = input.pn; #if 0 if (!(p2es = (p2e_t *) malloc (sizeof (p2e_t) * (p2en = edgen * 2)))) { prerror ("cannot malloc p2es"); abort (); } for (ei = 0, p2ei = 0; ei < edgen; ei++) { if (edges[ei].a.x == edges[ei].b.x && edges[ei].a.y == edges[ei].b.y) continue; p2es[p2ei].pp = &edges[ei].a; p2es[p2ei++].ep = &edges[ei]; p2es[p2ei].pp = &edges[ei].b; p2es[p2ei++].ep = &edges[ei]; } p2en = p2ei; qsort (p2es, p2en, sizeof (p2e_t), cmpp2efunc); elist = NULL; for (p2ei = 0; p2ei < p2en; p2ei += 2) { pp = p2es[p2ei].pp; #if DEBUG >= 1 fprintf (stderr, "point: %d %lf %lf\n", p2ei, pp->x, pp->y); #endif e0p = p2es[p2ei].ep; e1p = p2es[p2ei + 1].ep; p0 = (&e0p->a == p2es[p2ei].pp) ? e0p->b : e0p->a; p1 = (&e0p->a == p2es[p2ei + 1].pp) ? e1p->b : e1p->a; if (LT (p0, pp) && LT (p1, pp)) { listdelete (e0p), listdelete (e1p); } else if (GT (p0, pp) && GT (p1, pp)) { listinsert (e0p, *pp), listinsert (e1p, *pp); } else { if (LT (p0, pp)) listreplace (e0p, e1p); else listreplace (e1p, e0p); } } #endif /* generate the splines */ evs[0] = normv (evs[0]); evs[1] = normv (evs[1]); opl = 0; growops (4); ops[opl++] = inps[0]; if (reallyroutespline (edges, edgen, inps, inpn, evs[0], evs[1]) == -1) return -1; output->pn = opl; output->ps = ops; #if 0 fprintf (stderr, "edge\na\nb\n"); fprintf (stderr, "points\n%d\n", inpn); for (ipi = 0; ipi < inpn; ipi++) fprintf (stderr, "%f %f\n", inps[ipi].x, inps[ipi].y); fprintf (stderr, "splpoints\n%d\n", opl); for (opi = 0; opi < opl; opi++) fprintf (stderr, "%f %f\n", ops[opi].x, ops[opi].y); #endif return 0; }
Lightmap::Lightmap(Heightmap *orghm, int level, int shadowLevelDif, LightingInfo *li) { int startTicks = SDL_GetTicks(); tilesize.x = orghm->w-1; tilesize.y = orghm->h-1; name = "lightmap"; Heightmap *hm; int w; for(;;) { hm = orghm->GetLevel(-level); w=hm->w-1; GLint maxw; glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxw); if (w > maxw) level ++; else break; } shadowLevelDif=0; Heightmap *shadowhm = orghm->GetLevel(-(level+shadowLevelDif)); int shadowScale=1<<shadowLevelDif; int shadowW=shadowhm->w-1; assert (w/shadowW == shadowScale); //float org2c = w/float(orghm->w-1); //float c2org = (float)(orghm->w-1)/w; float *centerhm = SAFE_NEW float[w*w]; Vector3 *shading = SAFE_NEW Vector3[w*w]; for (int y=0;y<w;y++) for (int x=0;x<w;x++) { centerhm[y*w+x] =/* hm->scale * */ 0.25f * ( (int)hm->at(x,y)+ (int)hm->at(x+1,y)+ (int)hm->at(x,y+1) + (int)hm->at(x+1,y+1) ); //+ hm->offset; shading[y*w+x] = li->ambient; } uchar *lightMap = SAFE_NEW uchar[shadowW*shadowW]; for (std::vector<StaticLight>::const_iterator l=li->staticLights.begin();l!=li->staticLights.end();++l) { float lightx; float lighty; if (l->directional) { lightx = l->position.x; lighty = l->position.y; } else { lightx = (int)(l->position.x / shadowhm->squareSize); lighty = (int)(l->position.z / shadowhm->squareSize); } CalculateShadows(lightMap, shadowW, lightx, lighty, l->position.y, centerhm, w, shadowScale, l->directional); for (int y=0;y<w;y++) { for (int x=0;x<w;x++) { if (!lightMap[(y*shadowW+x)/shadowScale]) continue; Vector3 wp; if (l->directional) wp = l->position; else wp = l->position - Vector3((x+0.5f)*hm->squareSize,centerhm[y*w+x],(y+0.5f)*hm->squareSize); uchar* normal = hm->GetNormal (x,y); Vector3 normv((2 * (int)normal[0] - 256)/255.0f, (2 * (int)normal[1] - 256)/255.0f, (2 * (int)normal[2] - 256)/255.0f); wp.Normalize(); float dot = wp.dot(normv); if(dot < 0.0f) dot = 0.0f; if(dot > 1.0f) dot = 1.0f; dot *= lightMap[(y*shadowW+x)/shadowScale]*(1.0f/255.0f); shading[y*w+x] += l->color * dot; } } } delete[] lightMap; glGenTextures(1,&shadingTex); glBindTexture (GL_TEXTURE_2D, shadingTex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); uchar *shadingTexData=SAFE_NEW uchar[w*w*4]; for(int y=0;y<w;y++) { for (int x=0;x<w;x++) { shadingTexData[(y*w+x)*4+0] = (uchar)(min(1.0f, shading[y*w+x].x) * 255); shadingTexData[(y*w+x)*4+1] = (uchar)(min(1.0f, shading[y*w+x].y) * 255); shadingTexData[(y*w+x)*4+2] = (uchar)(min(1.0f, shading[y*w+x].z) * 255); shadingTexData[(y*w+x)*4+3] = CReadMap::EncodeHeight(centerhm[w*y+x]); } } SaveImage ("lightmap.png", 4, IL_UNSIGNED_BYTE, w,w, shadingTexData); glBuildMipmaps(GL_TEXTURE_2D, 4, w,w, GL_RGBA, GL_UNSIGNED_BYTE, shadingTexData); delete[] shadingTexData; id = shadingTex; delete[] shading; delete[] centerhm; int numTicks = SDL_GetTicks() - startTicks; d_trace ("Lightmap generation: %2.3f seconds\n", numTicks * 0.001f); }
int main(int argc, char* argv[]){ // parsing the arguments if(argc < 3) error_exit("Syntax error : Incorrect arguments, use : input_file output_file [-v] [-m filename].\r\n\r\n"); bool use_verbatim = false; FILE* matlab_file = NULL; FILE* input_file = fopen(argv[1],"r"); FILE* output_file = fopen(argv[2],"w+"); if( input_file == NULL || output_file == NULL) error_exit("Runtime error : could not open required i/o files.\r\n\r\n"); for(int i=3 ; i < argc; i++){ if( argv[i][0] != '-' ) error_exit("Syntax error : Unknown parameter : %s\r\n\r\n",argv[i]); switch(argv[i][1]){ case 'v' : use_verbatim = true; break; case 'm' : if(i+1 >= argc) error_exit("Syntax error : Filename required with -m option\r\n\r\n"); matlab_file = fopen(argv[i+1], "w+"); if(matlab_file == NULL) error_exit("Runtime error : Cannot open file '%s' for matlab output.\r\n\r\n",argv[i+1]); i++; break; } } // positions and speed at time n-1 (0) and n (1). vector p10,p20,v1,v2,p11,p21; // masses long double m1,m2; // discretization parameters long double dt, t_final, dt_output; fscanf(input_file, "%Lf\r\n%Lf %Lf %Lf\r\n%Lf %Lf %Lf\r\n%Lf\r\n%Lf %Lf %Lf\r\n%Lf %Lf %Lf\r\n%Lf\r\n%Lf\r\n%Lf", &m1, &(p10.x), &(p10.y), &(p10.z), &(v1.x), &(v1.y), &(v1.z), &m2, &(p20.x), &(p20.y), &(p20.z), &(v2.x), &(v2.y), &(v2.z), &dt, &t_final, &dt_output ); // Initial values determination p11 = addv(scalev(v1, dt), p10); p21 = addv(scalev(v2, dt), p20); write_init(output_file, matlab_file, use_verbatim, m1, p10, v1, m2, p20, v2, dt, t_final, dt_output); write_output(output_file, matlab_file, use_verbatim, 0, p10, p20); if(dt_output <= dt + DOUBLE_EPSILON) write_output(output_file, matlab_file, use_verbatim, dt, p11, p21); long double next_out = dt_output; for(long double t = dt * 2 ; t <= t_final ; t+= dt ){ vector dp = subv(p21,p11); long double norm = normv(dp); // compute F vector f = scalev(dp, - m1 * m2 * CONST_G / (norm * norm * norm)); // compute positions at time t vector new_p1 = addv(subv(scalev(f, - dt * dt / m1), p10) , scalev(p11, 2)); vector new_p2 = addv(subv(scalev(f, dt * dt / m2), p20) , scalev(p21, 2)); p10 = p11; p20 = p21; p11 = new_p1; p21 = new_p2; if(t + dt - DOUBLE_EPSILON >= next_out){ write_output(output_file, matlab_file, use_verbatim, t, p11, p21); next_out +=dt_output; } } write_end(output_file, matlab_file, use_verbatim); fclose(input_file); fclose(output_file); return EXIT_SUCCESS; }