// 3d trilinear interpolation void interpolation( double* U, double* U_fine, cuint I, cuint J, cuint K, cuint I_fine, cuint J_fine, cuint K_fine) { uint box_old[2][2][2]; uint box_fine[2][2][2]; #pragma omp parallel for shared(U, U_fine) private(box_old, box_fine) num_threads(nt) for(int i=0; i<I; i++){ for(int j=0; j<J; j++){ for(int k=0; k<K; k++){ // get the node nubmers of the old (coarse) box get_box(box_old, i,j,k, I, J, K); // get the node nubmbers of new (fine) box get_box(box_fine, i*2,j*2,k*2, I_fine, J_fine, K_fine); // map from coarse to fine grid fine_map(U, U_fine, box_old, box_fine); } } } // global constraints U_fine[I_fine*J_fine*K_fine] = U[I*J*K]/(I*J*K)*I_fine*J_fine*K_fine; // global constraint return; }
t_list *get_box_players(uint x, uint y) { t_box *box; if (!(box = get_box(x, y))) return (false); return (box->players); }
static void peasdemo_hello_world_plugin_deactivate (PeasActivatable *activatable) { PeasDemoHelloWorldPlugin *plugin = PEASDEMO_HELLO_WORLD_PLUGIN (activatable); g_debug ("%s", G_STRFUNC); gtk_container_remove (GTK_CONTAINER (get_box (plugin->window)), plugin->label); }
void set_box_delegg(t_egg * egg) { t_box * box; if (!(box = get_box(egg->x, egg->y)) || !del_node_as_arg(&box->eggs, match_egg, egg)) return ; free(egg); }
bool set_box_addfood(uint x, uint y, uint nb) { t_box *box; if (!(box = get_box(x, y))) return (false); box->food += nb; return (true); }
static void peasdemo_second_time_deactivate (PeasActivatable *activatable) { PeasDemoSecondTime *plugin = PEASDEMO_SECOND_TIME (activatable); g_debug ("%s", G_STRFUNC); gtk_container_remove (GTK_CONTAINER (get_box (plugin->window)), plugin->label); }
void CPhysicsShellHolder::correct_spawn_pos() { VERIFY (PPhysicsShell()); if( H_Parent() ) { CPhysicsShellHolder * P = smart_cast<CPhysicsShellHolder*>(H_Parent()); if( P && P->has_shell_collision_place(this) ) return; } Fvector size; Fvector c; get_box (PPhysicsShell(),XFORM(),size,c); R_ASSERT2( _valid( c ), make_string( "object: %s model: %s ", cName().c_str(), cNameVisual().c_str() ) ); R_ASSERT2( _valid( size ), make_string( "object: %s model: %s ", cName().c_str(), cNameVisual().c_str() ) ); R_ASSERT2( _valid( XFORM() ), make_string( "object: %s model: %s ", cName().c_str(), cNameVisual().c_str() ) ); CPHActivationShape activation_shape; activation_shape.Create (c,size,this); activation_shape.set_rotation (XFORM()); PPhysicsShell()->DisableCollision (); activation_shape.Activate (size,1,1.f,M_PI/8.f); //// VERIFY (valid_pos(activation_shape.Position(),phBoundaries)); // if (!valid_pos(activation_shape.Position(),phBoundaries)) { // CPHActivationShape activation_shape; // activation_shape.Create (c,size,this); // activation_shape.set_rotation (XFORM()); // activation_shape.Activate (size,1,1.f,M_PI/8.f); //// VERIFY (valid_pos(activation_shape.Position(),phBoundaries)); // } PPhysicsShell()->EnableCollision (); Fvector ap = activation_shape.Position(); #ifdef DEBUG if (!valid_pos(ap,phBoundaries)) { Msg("not valid position %f,%f,%f",ap.x,ap.y,ap.z); Msg("size %f,%f,%f",size.x,size.y,size.z); Msg("Object: %s",Name()); Msg("Visual: %s",*(cNameVisual())); Msg("Object pos %f,%f,%f",Position().x,Position().y,Position().z); } #endif // DEBUG VERIFY (valid_pos(activation_shape.Position(),phBoundaries)); Fmatrix trans; trans.identity (); trans.c.sub (ap,c); PPhysicsShell()->TransformPosition (trans); PPhysicsShell()->GetGlobalTransformDynamic(&XFORM()); activation_shape.Destroy (); }
static void peasdemo_hello_world_plugin_activate (PeasActivatable *activatable) { PeasDemoHelloWorldPlugin *plugin = PEASDEMO_HELLO_WORLD_PLUGIN (activatable); g_debug ("%s", G_STRFUNC); plugin->label = gtk_label_new ("Hello World!"); gtk_box_pack_start (get_box (plugin->window), plugin->label, 1, 1, 0); gtk_widget_show (plugin->label); g_object_ref (plugin->label); }
static void peasdemo_second_time_activate (PeasActivatable *activatable) { PeasDemoSecondTime *plugin = PEASDEMO_SECOND_TIME (activatable); g_debug ("%s", G_STRFUNC); plugin->label = gtk_label_new ("A second time!"); gtk_box_pack_start (get_box (plugin->window), plugin->label, 1, 1, 0); gtk_widget_show (plugin->label); g_object_ref (plugin->label); }
int get_action(double x, /* system variables == state information */ double x_dot, double theta, double theta_dot, double reinf) /* reinforcement signal */ { int i,j; double predicted_value; /* max_{b} Q(t, ss, b) */ if (first_time) { first_time = 0; reset_controller(); /* set state and action to null values */ for (i = 0; i < NUM_BOXES; i++) for (j = 0; j < 2; j++) q_val[i][j] = W_INIT; printf("... setting learning parameter ALPHA to %.4f.\n", ALPHA); printf("... setting noise parameter BETA to %.4f.\n", BETA); printf("... setting discount parameter GAMMA to %.4f.\n", GAMMA); printf("... random RND_SEED is %d.\n", RND_SEED); srand(RND_SEED); /* initialize random number generator */ } prev_state = cur_state; prev_action = cur_action; cur_state = get_box(x, x_dot, theta, theta_dot); if (prev_action != -1) /* Update, but not for first action in trial */ { if (cur_state == -1) /* failure state has Q-value of 0, since the value won't be updated */ predicted_value = 0.0; else if (q_val[cur_state][0] <= q_val[cur_state][1]) predicted_value = q_val[cur_state][1]; else predicted_value = q_val[cur_state][0]; q_val[prev_state][prev_action] += ALPHA * (reinf + GAMMA * predicted_value - q_val[prev_state][prev_action]); } /* Now determine best action */ if (q_val[cur_state][0] + rnd(-BETA, BETA) <= q_val[cur_state][1]) cur_action = 1; else cur_action = 0; return cur_action; }
void CPhysicsShellHolder::correct_spawn_pos() { VERIFY (PPhysicsShell()); Fvector size; Fvector c; get_box (PPhysicsShell(),XFORM(),size,c); CPHActivationShape activation_shape; activation_shape.Create (c,size,this); activation_shape.set_rotation (XFORM()); PPhysicsShell()->DisableCollision (); activation_shape.Activate (size,1,1.f,M_PI/8.f); //// VERIFY (valid_pos(activation_shape.Position(),phBoundaries)); // if (!valid_pos(activation_shape.Position(),phBoundaries)) { // CPHActivationShape activation_shape; // activation_shape.Create (c,size,this); // activation_shape.set_rotation (XFORM()); // activation_shape.Activate (size,1,1.f,M_PI/8.f); //// VERIFY (valid_pos(activation_shape.Position(),phBoundaries)); // } PPhysicsShell()->EnableCollision (); Fvector ap = activation_shape.Position(); #ifdef DEBUG if (!valid_pos(ap,phBoundaries)) { Msg("not valid position %f,%f,%f",ap.x,ap.y,ap.z); Msg("size %f,%f,%f",size.x,size.y,size.z); Msg("Object: %s",Name()); Msg("Visual: %s",*(cNameVisual())); Msg("Object pos %f,%f,%f",Position().x,Position().y,Position().z); } #endif // DEBUG VERIFY (valid_pos(activation_shape.Position(),phBoundaries)); Fmatrix trans; trans.identity (); trans.c.sub (ap,c); PPhysicsShell()->TransformPosition (trans); PPhysicsShell()->GetGlobalTransformDynamic(&XFORM()); activation_shape.Destroy (); }
t_egg *set_box_addegg(t_player *player) { t_player *p; t_box *box; t_egg *egg; if (!player || !(box = get_box(player->x, player->y))) return (NULL); if (!player->team || !(p = init_player(player->team))) return (NULL); player->team->max_conn += 1; p->x = player->x; p->y = player->y; if (!(egg = new_egg(player, p->id)) || !put_in_list(&(box->eggs), egg)) return (NULL); egg->himself = p; return (egg); }
bool CCharacterPhysicsSupport::CollisionCorrectObjPos(const Fvector& start_from,bool character_create/*=false*/) { //Fvector shift;shift.sub( start_from, m_EntityAlife.Position() ); Fvector shift;shift.set(0,0,0); Fbox box; if(character_create) box.set( movement()->Box() ); else { if(m_pPhysicsShell) { VERIFY(m_pPhysicsShell->isFullActive()); Fvector sz,c; get_box( m_pPhysicsShell, mXFORM, sz, c ); box.setb( Fvector().sub( c, m_EntityAlife.Position() ), Fvector(sz).mul(0.5f) ); m_pPhysicsShell->DisableCollision(); }else box.set( m_EntityAlife.BoundingBox() ); } Fvector vbox;Fvector activation_pos; box.get_CD(activation_pos,vbox); shift.add(activation_pos); vbox.mul(2.f); activation_pos.add(shift,m_EntityAlife.Position()); CPHActivationShape activation_shape; activation_shape.Create(activation_pos,vbox,&m_EntityAlife); if( !DoCharacterShellCollide() && !character_create ) { CPHCollideValidator::SetCharacterClassNotCollide(activation_shape); } if( !character_create ) activation_shape.set_rotation( mXFORM ); bool ret = activation_shape.Activate(vbox,1,1.f,M_PI/8.f); m_EntityAlife.Position().sub(activation_shape.Position(),shift); activation_shape.Destroy(); if(m_pPhysicsShell) m_pPhysicsShell->EnableCollision(); return ret; }
void main() { int action,box,i; long success,trial; double x, x_dot, theta, theta_dot,reinf,predicted_value; FILE *fptr; fptr=fopen("rand_restart.txt","w"); x=x_dot=theta=theta_dot=rnd(-BETA,BETA); success=0; trial=1; reinf=0.0; while (success<1000000) /* If the pole doesn't fall during 1-million trials,assume it succcess.*/ { action=get_action(x,x_dot,theta,theta_dot,reinf); cart_pole(action,&x,&x_dot,&theta,&theta_dot); box=get_box(x,x_dot,theta,theta_dot); if (box==-1) { reinf=-1.0; predicted_value = 0.0; q_val[prev_state][prev_action] += ALPHA * (reinf + GAMMA * predicted_value - q_val[prev_state][prev_action]); reset_controller(); x=x_dot=theta=theta_dot=rnd(-BETA,BETA); trial++; printf("At %d success ,try %d trials\n",success,trial); fprintf(fptr,"%d\t%d\n",trial,success); success=0; }else{ success++; reinf=0.0; } } printf("Success at %d trials \n",trial); for (i=0;i<NUM_BOXES;i++) fprintf(fptr,"%g %g\n",q_val[i][0],q_val[i][1]); fclose(fptr); }
boost::split(wkt_boxes, wkt_box_list, boost::is_any_of(";"), boost::token_compress_on); typedef box_item<Box> sample; std::vector<sample> boxes; int index = 1; BOOST_FOREACH(std::string const& wkt, wkt_boxes) { boxes.push_back(sample(index++, wkt)); } box_visitor<Box> visitor; bg::partition < Box >::apply(boxes, visitor, get_box(), ovelaps_box(), 1); BOOST_CHECK_CLOSE(visitor.area, expected_area, 0.001); BOOST_CHECK_EQUAL(visitor.count, expected_count); } struct point_item { point_item() : id(0) {} int id; double x;
int main(int argc, char **argv) { SDL_Event event; Uint8 mstat; int xw, yh, optc; int bombs = -1, width = -1, height = -1; struct option const longopts[] = { {"width", 1, NULL, 'w'}, {"height", 1, NULL, 'h'}, {"bombs", 1, NULL, 'b'}, {"help", 0, NULL, 'H'}, {NULL, 0, NULL, 0} }; progname = argv[0]; while ((optc = getopt_long(argc, argv, "w:h:b:H", longopts, NULL)) != -1) { switch (optc) { case 'w': width = atoi(optarg); break; case 'h': height = atoi(optarg); break; case 'b': bombs = atoi(optarg); break; case 'H': usage(0); exit(EXIT_SUCCESS); default: usage(1); exit(EXIT_FAILURE); } } usage(0); /* Use default value if invalid argument is given. */ if (width < 1 || width > 99) width = W_DEF; if (height < 1 || height > 99) height = H_DEF; if (bombs < 1 || bombs > width*height) bombs = width*height / 6; if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "Error: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } atexit(SDL_Quit); init_graphic(width, height); atexit(deinit_graphic); init_game(width, height, bombs); atexit(deinit_game); start_game(); while (SDL_WaitEvent(&event) >= 0) { switch (event.type) { case SDL_MOUSEBUTTONDOWN: if (is_gameover()) { start_game(); break; } xw = event.motion.x; yh = event.motion.y; pixel2pos(&xw, &yh); mstat = SDL_GetMouseState(NULL, NULL); if(mstat & SDL_BUTTON(1) || mstat & SDL_BUTTON(2)) open_box(get_box(xw, yh)); else if (mstat & SDL_BUTTON(3)) set_flag(get_box(xw, yh)); break; case SDL_QUIT: putchar('\n'); exit(EXIT_SUCCESS); break; default: break; /* Nothing. */ } } return 0; }
main() { float x, /* cart position, meters */ x_dot, /* cart velocity */ theta, /* pole angle, radians */ theta_dot; /* pole angular velocity */ vector w, /* vector of action weights */ v, /* vector of critic weights */ e, /* vector of action weight eligibilities */ xbar; /* vector of critic weight eligibilities */ float p, oldp, rhat, r; int box, i, y, steps = 0, failures=0, failed; printf("Seed? "); scanf("%d",&i); srand(i); //srand (time(NULL)); /*--- Initialize action and heuristic critic weights and traces. ---*/ for (i = 0; i < N_BOXES; i++) w[i] = v[i] = xbar[i] = e[i] = 0.0; /*--- Starting state is (0 0 0 0) ---*/ x = x_dot = theta = theta_dot = 0.0; /*--- Find box in state space containing start state ---*/ box = get_box(x, x_dot, theta, theta_dot); /*--- Iterate through the action-learn loop. ---*/ while (steps++ < MAX_STEPS && failures < MAX_FAILURES) { /*--- Choose action randomly, biased by current weight. ---*/ y = (random < prob_push_right(w[box])); /*--- Update traces. ---*/ e[box] += (1.0 - LAMBDAw) * (y - 0.5); xbar[box] += (1.0 - LAMBDAv); /*--- Remember prediction of failure for current state ---*/ oldp = v[box]; /*--- Apply action to the simulated cart-pole ---*/ cart_pole(y, &x, &x_dot, &theta, &theta_dot); /*--- Get box of state space containing the resulting state. ---*/ box = get_box(x, x_dot, theta, theta_dot); if (box < 0) { /*--- Failure occurred. ---*/ failed = 1; failures++; printf("Trial %d was %d steps.\n", failures, steps); steps = 0; /*--- Reset state to (0 0 0 0). Find the box. ---*/ x = x_dot = theta = theta_dot = 0.0; box = get_box(x, x_dot, theta, theta_dot); /*--- Reinforcement upon failure is -1. Prediction of failure is 0. ---*/ r = -1.0; p = 0.; } else { /*--- Not a failure. ---*/ failed = 0; /*--- Reinforcement is 0. Prediction of failure given by v weight. ---*/ r = 0; p= v[box]; } /*--- Heuristic reinforcement is: current reinforcement + gamma * new failure prediction - previous failure prediction ---*/ rhat = r + GAMMA * p - oldp; for (i = 0; i < N_BOXES; i++) { /*--- Update all weights. ---*/ w[i] += ALPHA * rhat * e[i]; v[i] += BETA * rhat * xbar[i]; if (v[i] < -1.0) v[i] = v[i]; if (failed) { /*--- If failure, zero all traces. ---*/ e[i] = 0.; xbar[i] = 0.; } else { /*--- Otherwise, update (decay) the traces. ---*/ e[i] *= LAMBDAw; xbar[i] *= LAMBDAv; } } } if (failures == MAX_FAILURES) printf("Pole not balanced. Stopping after %d failures.",failures); else printf("Pole balanced successfully for at least %d steps\n", steps); }
int main() { int action,box,i; long success,trial; double x, x_dot, theta, theta_dot,reinf,predicted_value; FILE *fptr; FILE *fptr1; fptr=fopen("rand_restart.txt","w"); fptr1=fopen("output.csv","w"); x=x_dot=theta=theta_dot=rnd(-BETA,BETA); double angle; success=0; trial=1; reinf=0.0; double force; double j,k; double best_ALPHA=0; double best_GAMMA=0; while (success<1000000) /* If the pole doesn't fall during 1-million trials,assume it succcess.*/ { //getchar(); action=get_action(x,x_dot,theta,theta_dot,reinf); cart_pole(action,&x,&x_dot,&theta,&theta_dot); //printf("%d") if(action==0) force=10; else if(action==1) force=5; else if(action==2) force=0; else if(action==3) force=-5; else force=-10; fprintf(fptr1,"%.2f,%.2f,%.2f,%.2f,%f\n",x,theta,x_dot,theta_dot,force); angle=theta*180/3.1415926; //printf("x%.2f,angle%.2f,%.2f,%.2f,%d\n",x,angle,x_dot,theta_dot,action); box=get_box(x,x_dot,theta,theta_dot); if (box==-1) { reinf=-1.0; predicted_value = 0.0; q_val[prev_state][prev_action] += ALPHA * (reinf + GAMMA * predicted_value - q_val[prev_state][prev_action]); reset_controller(); x=x_dot=theta=theta_dot=rnd(-BETA,BETA); trial++; //printf("At %d success ,try %d trials\n",success,trial); printf("At trial %d : success--->%d (mean last how long)\n",trial,success); fprintf(fptr,"trials%d\t success%d\n",trial,success); success=0; }else{ success++; reinf=0.0; /*if(success>1000000-2) { printf("asfasdfasdf"); break; }*/ } } printf("If success > 1000000 \n Success at %d trials \n",trial); for (i=0;i<NUM_BOXES;i++) fprintf(fptr,"%g %g\n",q_val[i][0],q_val[i][1]); fclose(fptr); fclose(fptr1); system("pause"); }
static int tool_brush_iter(goxel_t *goxel, const inputs_t *inputs, int state, const vec2_t *view_size, bool inside) { const bool down = inputs->mouse_down[0]; const bool pressed = down && !goxel->painting; const bool released = !down && goxel->painting; int snaped = 0; vec3_t pos, normal; box_t box; painter_t painter2; mesh_t *mesh = goxel->image->active_layer->mesh; if (inside) snaped = goxel_unproject(goxel, view_size, &inputs->mouse_pos, &pos, &normal); goxel_set_help_text(goxel, "Brush: use shift to draw lines, " "ctrl to pick color"); if (snaped) { if ( snaped == SNAP_MESH && goxel->painter.op == OP_ADD && !goxel->snap_offset) vec3_iadd(&pos, normal); if (goxel->tool == TOOL_BRUSH && goxel->snap_offset) vec3_iaddk(&pos, normal, goxel->snap_offset * goxel->tool_radius); pos.x = round(pos.x - 0.5) + 0.5; pos.y = round(pos.y - 0.5) + 0.5; pos.z = round(pos.z - 0.5) + 0.5; } if (state == STATE_IDLE) { goxel->tool_t = 0; if (snaped) state = STATE_SNAPED; } if (state == STATE_SNAPED) { if (goxel->tool_t == 0) { goxel->tool_t = 1; mesh_set(&goxel->tool_origin_mesh, mesh); if (!inputs->keys[KEY_SHIFT]) mesh_set(&goxel->pick_mesh, goxel->layers_mesh); goxel->tool_last_op.op = 0; // Discard last op. } if (!snaped) return STATE_CANCEL; if (inputs->keys[KEY_SHIFT]) render_line(&goxel->rend, &goxel->tool_start_pos, &pos, NULL); if (check_can_skip(goxel, pos, down, goxel->painter.op)) return state; box = get_box(&pos, NULL, &normal, goxel->tool_radius, NULL); mesh_set(&mesh, goxel->tool_origin_mesh); mesh_op(mesh, &goxel->painter, &box); goxel_update_meshes(goxel, false); if (inputs->keys[KEY_SHIFT]) { render_line(&goxel->rend, &goxel->tool_start_pos, &pos, NULL); if (pressed) { painter2 = goxel->painter; painter2.shape = &shape_cylinder; box = get_box(&goxel->tool_start_pos, &pos, &normal, goxel->tool_radius, NULL); mesh_op(mesh, &painter2, &box); goxel_update_meshes(goxel, false); goxel->tool_start_pos = pos; mesh_set(&goxel->tool_origin_mesh, mesh); } } if (pressed) { mesh_set(&mesh, goxel->tool_origin_mesh); state = STATE_PAINT; goxel->tool_last_op.op = 0; goxel->painting = true; } } if (state == STATE_PAINT) { if (check_can_skip(goxel, pos, down, goxel->painter.op)) return state; if (released) { image_history_push(goxel->image); goxel->painting = false; goxel->last_pos = pos; if (inputs->keys[KEY_SHIFT]) return STATE_WAIT_KEY_UP; mesh_set(&goxel->pick_mesh, goxel->layers_mesh); return STATE_IDLE; } box = get_box(&pos, NULL, &normal, goxel->tool_radius, NULL); mesh_op(mesh, &goxel->painter, &box); goxel_update_meshes(goxel, false); goxel->tool_start_pos = pos; } if (state == STATE_WAIT_KEY_UP) { goxel->tool_t = 0; if (!inputs->keys[KEY_SHIFT]) state = STATE_IDLE; if (snaped) state = STATE_SNAPED; } return state; }
static int tool_cube_iter(goxel_t *goxel, const inputs_t *inputs, int state, const vec2_t *view_size, bool inside) { const bool down = inputs->mouse_down[0]; const bool up = !down; int snaped = 0; vec3_t pos, normal; box_t box; uvec4b_t box_color = HEXCOLOR(0xffff00ff); mesh_t *mesh = goxel->image->active_layer->mesh; if (inside) snaped = goxel_unproject(goxel, view_size, &inputs->mouse_pos, &pos, &normal); if (snaped) { if ( snaped == SNAP_MESH && goxel->painter.op == OP_ADD && !goxel->snap_offset) vec3_iadd(&pos, normal); pos.x = round(pos.x - 0.5) + 0.5; pos.y = round(pos.y - 0.5) + 0.5; pos.z = round(pos.z - 0.5) + 0.5; } if (state == STATE_IDLE) { goxel->tool_t = 0; if (snaped) state = STATE_SNAPED; } if (state == STATE_SNAPED) { if (goxel->tool_t == 0) { goxel->tool_t = 1; mesh_set(&goxel->tool_origin_mesh, mesh); } if (!snaped) return STATE_CANCEL; goxel_set_help_text(goxel, "Click and drag to draw."); goxel->tool_start_pos = pos; box = get_box(&goxel->tool_start_pos, &pos, &normal, 0, &goxel->plane); mesh_set(&mesh, goxel->tool_origin_mesh); mesh_op(mesh, &goxel->painter, &box); render_box(&goxel->rend, &box, false, &box_color, false); if (down) { state = STATE_PAINT; goxel->painting = true; } } if (state == STATE_PAINT) { goxel_set_help_text(goxel, "Drag."); box = get_box(&goxel->tool_start_pos, &pos, &normal, 0, &goxel->plane); render_box(&goxel->rend, &box, false, &box_color, false); mesh_set(&mesh, goxel->tool_origin_mesh); mesh_op(mesh, &goxel->painter, &box); goxel_update_meshes(goxel, false); if (up) { state = STATE_PAINT2; goxel->tool_plane = plane_from_normal(pos, goxel->plane.u); } } if (state == STATE_PAINT2) { goxel_set_help_text(goxel, "Adjust height."); render_plane(&goxel->rend, &goxel->tool_plane, &goxel->grid_color); pos = vec3_add(goxel->tool_plane.p, vec3_project(vec3_sub(pos, goxel->tool_plane.p), goxel->plane.n)); box = get_box(&goxel->tool_start_pos, &pos, &normal, 0, &goxel->plane); render_box(&goxel->rend, &box, false, &box_color, false); mesh_set(&mesh, goxel->tool_origin_mesh); mesh_op(mesh, &goxel->painter, &box); goxel_update_meshes(goxel, false); if (down) { mesh_set(&mesh, goxel->tool_origin_mesh); mesh_op(mesh, &goxel->painter, &box); goxel_update_meshes(goxel, true); goxel->painting = false; image_history_push(goxel->image); return STATE_WAIT_UP; } } if (state == STATE_WAIT_UP) { goxel->tool_plane = plane_null; if (up) state = STATE_IDLE; } return state; }
// XXX: this is very close to tool_cube_iter. static int tool_selection_iter(goxel_t *goxel, const inputs_t *inputs, int state, const vec2_t *view_size, bool inside) { extern const mat4_t FACES_MATS[6]; const bool down = inputs->mouse_down[0]; const bool up = !down; int snaped = 0; int face = -1; vec3_t pos, normal; plane_t face_plane; box_t box; uvec4b_t box_color = HEXCOLOR(0xffff00ff); // See if we can snap on a selection face. if (inside && !box_is_null(goxel->selection) && IS_IN(state, STATE_IDLE, STATE_SNAPED, STATE_SNAPED_FACE)) { goxel->tool_snape_face = -1; if (goxel_unproject_on_box(goxel, view_size, &inputs->mouse_pos, &goxel->selection, &pos, &normal, &face)) { goxel->tool_snape_face = face; state = STATE_SNAPED_FACE; } } if (!box_is_null(goxel->selection) && goxel->tool_snape_face != -1) face_plane.mat = mat4_mul(goxel->selection.mat, FACES_MATS[goxel->tool_snape_face]); if (inside && face == -1) snaped = goxel_unproject(goxel, view_size, &inputs->mouse_pos, &pos, &normal); if (snaped) { pos.x = round(pos.x - 0.5) + 0.5; pos.y = round(pos.y - 0.5) + 0.5; pos.z = round(pos.z - 0.5) + 0.5; } if (state == STATE_IDLE) { goxel->tool_t = 0; goxel->tool_snape_face = -1; if (snaped) state = STATE_SNAPED; } if (state == STATE_SNAPED) { if (!snaped) return STATE_CANCEL; goxel_set_help_text(goxel, "Click and drag to set selection."); goxel->tool_start_pos = pos; box = get_box(&goxel->tool_start_pos, &pos, &normal, 0, &goxel->plane); render_box(&goxel->rend, &box, false, &box_color, false); if (down) { state = STATE_PAINT; goxel->painting = true; } } if (state == STATE_PAINT) { goxel_set_help_text(goxel, "Drag."); goxel->selection = get_box(&goxel->tool_start_pos, &pos, &normal, 0, &goxel->plane); if (up) { state = STATE_PAINT2; goxel->tool_plane = plane_from_normal(pos, goxel->plane.u); } } if (state == STATE_PAINT2) { goxel_set_help_text(goxel, "Adjust height."); render_plane(&goxel->rend, &goxel->tool_plane, &goxel->grid_color); pos = vec3_add(goxel->tool_plane.p, vec3_project(vec3_sub(pos, goxel->tool_plane.p), goxel->plane.n)); goxel->selection = get_box(&goxel->tool_start_pos, &pos, &normal, 0, &goxel->plane); if (down) { goxel->painting = false; return STATE_WAIT_UP; } } if (state == STATE_WAIT_UP) { goxel->tool_plane = plane_null; goxel->selection = box_get_bbox(goxel->selection); return up ? STATE_IDLE : STATE_WAIT_UP; } if (IS_IN(state, STATE_SNAPED_FACE, STATE_MOVE_FACE)) goxel_set_help_text(goxel, "Drag to move face"); if (state == STATE_SNAPED_FACE) { if (face == -1) return STATE_IDLE; render_img(&goxel->rend, NULL, &face_plane.mat); if (down) { state = STATE_MOVE_FACE; goxel->tool_plane = plane(pos, normal, vec3_normalized(face_plane.u)); } } if (state == STATE_MOVE_FACE) { if (up) return STATE_IDLE; goxel_unproject_on_plane(goxel, view_size, &inputs->mouse_pos, &goxel->tool_plane, &pos, &normal); pos = vec3_add(goxel->tool_plane.p, vec3_project(vec3_sub(pos, goxel->tool_plane.p), vec3_normalized(face_plane.n))); pos.x = round(pos.x); pos.y = round(pos.y); pos.z = round(pos.z); goxel->selection = box_move_face(goxel->selection, goxel->tool_snape_face, pos); } return state; }
LB_API int lbind_getudtypebox(lua_State *L) { return get_box(L, LBIND_UDBOX); }
static void get_typebox(lua_State *L) { get_box(L, LBIND_TYPEBOX); }
static void get_internbox(lua_State *L) { if (get_box(L, LBIND_PTRBOX)) { lua_pushliteral(L, "v"); lbind_setmetafield(L, -2, "__mode"); } }
int main(int argc, char* argv[]) { int order,count,i,j,k,l; FILE *f1,*f2; double *m1,*m2,*dm; int ret=0; long numberOfValues; double geast,gwest,gnorth,gsouth; double *east,*west,*north,*south; grib_handle *h1,*h2; grib_context *c=grib_context_get_default(); int split=1; fprintf(stderr, "\nWARNING: The tool %s is deprecated.\n\n", argv[0]); if (argc!=5) usage(argv[0]); split=atoi(argv[1]); order=atoi(argv[2]); f1=fopen(argv[3],"r"); if (!f1) { perror(argv[3]); exit(1); } f2=fopen(argv[4],"r"); if (!f2) { perror(argv[4]); exit(1); } m1=(double*)grib_context_malloc_clear(c,sizeof(double)*order*order); m2=(double*)grib_context_malloc_clear(c,sizeof(double)*order*order); dm=(double*)grib_context_malloc_clear(c,sizeof(double)*order*order); east=(double*)grib_context_malloc_clear(c,sizeof(double)*split); south=(double*)grib_context_malloc_clear(c,sizeof(double)*split); north=(double*)grib_context_malloc_clear(c,sizeof(double)*split); west=(double*)grib_context_malloc_clear(c,sizeof(double)*split); count=0; while ((h1=grib_handle_new_from_file(0,f1,&ret))!=NULL && (h2=grib_handle_new_from_file(0,f2,&ret))!=NULL) { get_box(h1,&geast,&gnorth,&gwest,&gsouth); if (split>1) { double inc=0; double d=(gwest-geast)/split; inc=0; for (i=0;i<split;i++) { east[i]=geast+inc; inc+=d; west[i]=geast+inc; } d=(gnorth-gsouth)/split; inc=0; for (i=0;i<split;i++) { south[i]=gsouth+inc; inc+=d; north[i]=gsouth+inc; } } else { east[0]=geast; north[0]=gnorth; south[0]=gsouth; west[0]=gwest; } for (k=0;k<split;k++) { for (l=0;l<split;l++) { printf("- %d - east=%.3f \twest=%.3f \tsouth=%.3f \tnorth=%.3f\n",count+1,east[k],west[k],south[l],north[l]); grib_moments(h1,east[k],north[l],west[k],south[l],order,m1,&numberOfValues); grib_moments(h2,east[k],north[l],west[k],south[l],order,m2,&numberOfValues); printf("numberOfValues=%ld\n",numberOfValues); for (i=0;i<order;i++) { for (j=0;j<order;j++) { printf(" (%d,%d) ",i,j); } } printf("\n"); for (i=0;i<order*order;i++) printf("% .2e ",m1[i]); printf("\n"); for (i=0;i<order*order;i++) printf("% .2e ",m2[i]); printf("\n"); for (i=0;i<order*order;i++) printf("% .2e ",fabs((m1[i]-m2[i])/m2[i])); printf("\n\n"); } } grib_handle_delete(h2); grib_handle_delete(h1); count++; } grib_context_free(c,m1); grib_context_free(c,m2); grib_context_free(c,dm); grib_context_free(c,east); grib_context_free(c,south); grib_context_free(c,north); grib_context_free(c,west); fclose(f1); fclose(f2); return 0; }