void SMat<CoeffRing>::column2by2(size_t c1, size_t c2, elem a1, elem a2, elem b1, elem b2) /* column(c1) <- a1 * column(c1) + a2 * column(c2), column(c2) <- b1 * column(c1) + b2 * column(c2) */ { // Make first column: v1 = a1*c1+a2*c2 sparsevec *v1 = vec_copy(columns_[c1]); sparsevec *v2 = vec_copy(columns_[c2]); vec_scale(v1, a1); vec_scale(v2, a2); vec_add_to(v1, v2); // Second column: w1 = b1*c1 + b2*c2 sparsevec *w1 = columns_[c1]; sparsevec *w2 = columns_[c2]; vec_scale(w1, b1); vec_scale(w2, b2); vec_add_to(w1, w2); // Set the matrices: columns_[c1] = v1; columns_[c2] = w1; }
void mesh_init_vertex_normals(struct mesh *mesh) { struct polygon *p, *poly_end; struct vertex *v, *vtx_end; struct vector *poly_normal; int *idx; int nvtx; vtx_end = &mesh->vtx[mesh->nvtx]; for (v = mesh->vtx; v != vtx_end; v++) vec_zero(&v->normal); poly_end = &mesh->poly[mesh->npoly]; for (p = mesh->poly; p != poly_end; p++) { poly_normal = &p->normal; idx = p->vtx_index; nvtx = p->nvtx; while (nvtx--) vec_add_to(&mesh->vtx[*idx++].normal, poly_normal); } for (v = mesh->vtx; v != vtx_end; v++) vec_normalize(&v->normal); }
void SMat<CoeffRing>::vec_column_op(sparsevec *&v, const elem &a, sparsevec *w) const // v := v + a*w { sparsevec *w1 = vec_copy(w); vec_scale(w1, a); vec_add_to(v, w1); }
void SMat<CoeffRing>::addInPlace(const SMat<CoeffRing> &B) // return this + B. return NULL of sizes or types do not match. { assert(&B.ring() == &ring()); assert(B.numRows() == numRows()); assert(B.numColumns() == numColumns()); for (size_t c = 0; c < numColumns(); c++) { sparsevec *v = vec_copy(B.columns_[c]); vec_add_to(columns_[c], v); } }
void SMat<CoeffRing>::subtractInPlace(const SMat<CoeffRing> &B) // this -= B. // assumption:the assert statements below: { assert(&B.ring() == &ring()); assert(B.numRows() == numRows()); assert(B.numColumns() == numColumns()); for (size_t c = 0; c < numColumns(); c++) { sparsevec *v = vec_copy(B.columns_[c]); vec_negate(v); vec_add_to(columns_[c], v); } }
static int intersect_board_squares(struct hit *hit, const struct ray *ray, const struct vector *board_pos, const unsigned char *state, int size) { float t; float start_x, start_y, size_x, size_y; struct vector dir, hit_pos; /* find hit position */ vec_sub(&dir, &ray->to, &ray->from); if (dir.z == 0.f) return 0; t = (board_pos->z - ray->from.z)/dir.z; if (t < 0.f) return 0; vec_scalar_mul_copy(&hit_pos, &dir, t); vec_add_to(&hit_pos, &ray->from); /* figure out square position */ size_x = SQUARE_SIZE*size; size_y = SQUARE_SIZE*size; start_x = board_pos->x - .5f*size_x; start_y = board_pos->y - .5f*size_y; if (hit_pos.x < start_x || hit_pos.x > start_x + size_x) return 0; if (hit_pos.y < start_y || hit_pos.y > start_y + size_y) return 0; hit->t = t; hit->row = (hit_pos.x - start_x)/SQUARE_SIZE; hit->col = (hit_pos.y - start_y)/SQUARE_SIZE; return 1; }
void SMat<CoeffRing>::vec_sort(sparsevec *&f) const { if (f == 0 || f->next == 0) return; sparsevec *f1 = 0; sparsevec *f2 = 0; while (f != 0) { sparsevec *t = f; f = f->next; t->next = f1; f1 = t; if (f == 0) break; t = f; f = f->next; t->next = f2; f2 = t; } vec_sort(f1); vec_sort(f2); vec_add_to(f1, f2); f = f1; }