Exemple #1
0
static void __bounding_get_effective_position(const Bounding *b, Vector *result)
{
    assert(b && "Bad bounding pointer.");
    assert(b->origin && b->previous_origin && b->orientation && b->direction && "Bad bounding data.");
    assert(result && "Bad result pointer.");

    Vector p = *b->origin,
           t,
           side;
    if (!vector_eq(b->orientation, b->direction))
    {
        vector_vector_mul(b->orientation, b->direction, &side);
    }
    else
    {
        vector_get_orthogonal(b->direction, &side);
    }
    VECTOR_NORMALIZE(&side);

    double sx, sy, sz;
    vector_scale(b->direction, b->offset.x, &t);
    sx = vector_length(&t);
    vector_scale(&side, b->offset.y, &t);
    sy = vector_length(&t);
    vector_scale(b->orientation, b->offset.z, &t);
    sz = vector_length(&t);

    p.x += sx;
    p.y += sy;
    p.z += sz;

    *result = p;
}
Exemple #2
0
Vector vector_update_sample_variance(Vector var_n, Vector mean_n1, Vector x_n1, int n)
{
    if (n == 0)
        return ORIGIN;

     Vector a = vector_scale(var_n, (float) n / (n + 1));
     Vector b = vector_scale(vector_square_2(vector_sub(x_n1, mean_n1)), 1.0 / n);
     return vector_add(a, b);
}
Exemple #3
0
Vector vector_interpolate_spherical(Vector a, Vector b, float t)
{
    float phi = vector_angle(a, b);
    float s = sin(phi);

    float factor_a = sin((1 - t) * phi) / s;
    float factor_b = sin(t * phi) / s;

    return vector_add(vector_scale(a, factor_a), vector_scale(b, factor_b));
}
Exemple #4
0
static void update_particle(particle *p) {
  const generator_data *data = p->data;
  double factor = p->time_alive / p->time_to_live;
  double time = time_elapsed;
  p->velocity = vector_scale(p->velocity, 1 - data->deceleration * time);
  p->position = vector_add(p->position, vector_scale(p->velocity, time));
  p->time_alive += time;
  p->radius = data->start_radius + factor * data->end_radius;
  p->color = lerp_color(data->start_color, data->end_color, factor);
}
void	set_val_sphere(t_env *env, double t, t_ray *ray)
{
	OBJ.new_start = vector_add(ray->start, vector_scale(t, ray->dir));
	OBJ.normal = vector_sub(OBJ.new_start, SP_POS(OBJ.cur_sphere));
	if (vector_dot(OBJ.normal, OBJ.normal) == 0)
	{
		env->br = 1;
		return ;
	}
	OBJ.normal = vector_scale(1.0f / ABSV(OBJ.normal), OBJ.normal);
	OBJ.cur_mat = env->obj.mats[SPHERES[OBJ.cur_sphere].shape.material];
}
Exemple #6
0
void	lighting(t_intersect *sct, t_env *env, float *reflect)
{
	float	refl_ray;
	t_vec	tmp;
//	t_ray	r;

//	reset_ray(r, &env->r);
	sct->norm_check = 1.0f / sqrtf(sct->norm_check);
	sct->normal = vector_scale(sct->norm_check, &sct->normal);
	light_point(sct, env);
	env->coef *= *reflect;
	refl_ray = 2.0f * vector_dot(&env->r.dir, &sct->normal);
	tmp = vector_scale(refl_ray, &sct->normal);
	env->r.dir = vector_sub(&env->r.dir, &tmp);
}
Exemple #7
0
void	set_val_sphere(t_env *env, float t, t_ray ray)
{
	t_vector	scaled;

	scaled = vector_scale(t, &ray.dir);
	OBJ.new_start = vector_add(&ray.start, &scaled);
	OBJ.normal = vector_sub(&OBJ.new_start, &SP_POS(OBJ.cur_sphere));
	if (vector_dot(&OBJ.normal, &OBJ.normal) == 0)
	{
		env->br = 1;
		return ;
	}
	OBJ.normal = vector_scale(1.0f / ABSV(OBJ.normal), &OBJ.normal);
	OBJ.cur_mat = env->obj.mats[SPHERES[OBJ.cur_sphere].shape.material];
}
Exemple #8
0
void	set_val_tri(t_env *env, float t, t_ray ray)
{
	t_vector	scaled;

	scaled = vector_scale(t, &ray.dir);
	OBJ.new_start = vector_add(&ray.start, &scaled);
	OBJ.normal = TRI[OBJ.cur_tri].normal;
	if (vector_dot(&OBJ.normal, &OBJ.normal) == 0)
	{
		env->br = 1;
		return ;
	}
	OBJ.normal = vector_scale(1.0f / ABSV(OBJ.normal), &OBJ.normal);
	OBJ.cur_mat = env->obj.mats[TRI[OBJ.cur_tri].shape.material];
}
Exemple #9
0
int main(int argc, char *argv[]) {
  Point myPoint = point_new(0.0, 0.0);
  Vector unitX = vector_new(1.0, 0.0);
  Vector unitY = vector_new(0.0, 1.0);

  printf("%s + %s + %s = ", point_toString(myPoint),
                            vector_toString(unitX),
                            vector_toString(unitY) );

  point_addVector(myPoint, unitX);
  point_addVector(myPoint, unitY);

  printf("As a tuple: %s\n", tuple_toString((Tuple)myPoint));
  printf("As a point: %s\n", point_toString(myPoint));

  printf( "After scaling unitX by -0.5: %s\n",
	  vector_toString(vector_scale(unitX, -0.5))
	);
  printf("unitX has length %lf\n", vector_length(unitX));

  vector_delete(unitX);
  vector_delete(unitY);
  point_delete(myPoint);

  return 0;
}
Exemple #10
0
void generate_normals(object *obj){
	unsigned int mesh,i;
	unsigned short *face;

	if(!obj) return;

	for(i=0;i<obj->vertex_count;i++)
		obj->vertices[i].normal = vector_make(0,0,0);

	for(mesh=0;mesh<obj->submesh_count;mesh++){
		face = obj->submeshes[mesh].triangles;
		for(i=0;i<obj->submeshes[mesh].triangle_count;i++){
			vector facenormal = vector_scale(
									vector_crossproduct(
										vector_sub( obj->vertices[face[i*3+2]].vertex, obj->vertices[face[i*3+0]].vertex ),
										vector_sub( obj->vertices[face[i*3+1]].vertex, obj->vertices[face[i*3+0]].vertex )
									),-1
								);

			obj->vertices[face[i*3+0]].normal =  vector_add( obj->vertices[face[i*3+0]].normal, facenormal );
			obj->vertices[face[i*3+1]].normal =  vector_add( obj->vertices[face[i*3+1]].normal, facenormal );
			obj->vertices[face[i*3+2]].normal =  vector_add( obj->vertices[face[i*3+2]].normal, facenormal );
	
		}
	}

//	for(i=0;i<obj->vertex_count;i++)
//		obj->vertices[i].normal = vector_normalize(obj->vertices[i].normal);
}
static void midpt_set_domain(void* context, 
                             mesh_t* mesh, 
                             int cell)
{
  midpt_t* midpt = context;
  ASSERT(mesh_cell_num_faces(mesh, cell) < 50);
  midpt->num_faces = mesh_cell_num_faces(mesh, cell);

  // Fetch face centers/areas/normals and the cell center.
  midpt->cell_center = mesh->cell_centers[cell];
  midpt->cell_volume = mesh->cell_volumes[cell];
  int pos = 0, face;
  point_t* xc = &midpt->cell_center;
  while (mesh_cell_next_oriented_face(mesh, cell, &pos, &face))
  {
    int local_face = pos - 1;
    int actual_face = (face >= 0) ? face : ~face;
    midpt->face_areas[local_face] = mesh->face_areas[actual_face];
    midpt->face_centers[local_face] = mesh->face_centers[actual_face];

    // Use the orientation of the face to determine the direction of the 
    // face's normal vector.
    vector_t n = mesh->face_normals[actual_face];
    point_t* xf = &mesh->face_centers[actual_face];
    vector_t outward;
    point_displacement(xc, xf, &outward);
    if (face != actual_face)
      vector_scale(&n, -1.0);
    midpt->face_normals[local_face] = n;
  }
}
void	set_val_cone(t_env *env, double t, t_ray *ray)
{
	OBJ.new_start = vector_add(ray->start, vector_scale(t, ray->dir));
	OBJ.normal = get_cone_normal(CONES[OBJ.cur_cone], OBJ.new_start);
	OBJ.cur_mat = env->obj.mats[CONES[OBJ.cur_cone].shape.material];
	env->spec_coef = OBJ.cur_mat.specular;
}
Exemple #13
0
ok_status vector_uniform_rand(vector * v, const ok_float minval,
	const ok_float maxval)
{
	OK_RETURNIF_ERR( ok_rand_u01(v->data, v->size, v->stride) );
	OK_RETURNIF_ERR( vector_scale(v, maxval - minval) );
	return OK_SCAN_ERR( vector_add_constant(v, minval) );
}
Exemple #14
0
void k3 (ODEFunc ode, float *xn, float *d, float *k2, float time, float tdelt, int dim, float *scratch)
{
	float h2 = tdelt / 2.0f;
	vector_scale (k2, h2, scratch, dim);
	vector_add (scratch, xn, scratch, dim);
	ode (scratch, d, time + h2);
}
/* build orthonormal basis matrix
Q = Y;
for j=1:k
    vj = Q(:,j);
    for i=1:(j-1)
        vi = Q(:,i);
        vj = vj - project_vec(vj,vi);
    end
    vj = vj/norm(vj);
    Q(:,j) = vj;
end
*/
void build_orthonormal_basis_from_mat(mat *A, mat *Q){
    int m,n,i,j,ind,num_ortos=2;
    double vec_norm;
    vec *vi,*vj,*p;
    m = A->nrows;
    n = A->ncols;
    vi = vector_new(m);
    vj = vector_new(m);
    p = vector_new(m);
    matrix_copy(Q, A);

    for(ind=0; ind<num_ortos; ind++){
        for(j=0; j<n; j++){
            matrix_get_col(Q, j, vj);
            for(i=0; i<j; i++){
                matrix_get_col(Q, i, vi);
                project_vector(vj, vi, p);
                vector_sub(vj, p);
            }
            vec_norm = vector_get2norm(vj);
            vector_scale(vj, 1.0/vec_norm);
            matrix_set_col(Q, j, vj);
        }
    }
    vector_delete(vi);
    vector_delete(vj);
    vector_delete(p);
}
/*
% project v in direction of u
function p=project_vec(v,u)
p = (dot(v,u)/norm(u)^2)*u;
*/
void project_vector(vec *v, vec *u, vec *p){
    double dot_product_val, vec_norm, scalar_val; 
    dot_product_val = vector_dot_product(v, u);
    vec_norm = vector_get2norm(u);
    scalar_val = dot_product_val/(vec_norm*vec_norm);
    vector_copy(p, u);
    vector_scale(p, scalar_val); 
}
void	set_val_plane(t_env *env, double t, t_ray *ray)
{
	OBJ.new_start = vector_add(ray->start, vector_scale(t, ray->dir));
	OBJ.normal = OBJ.planes[OBJ.cur_plane].rot;
	if (vector_dot(ray->dir, OBJ.normal) > 0.0F)
		OBJ.normal = vector_sub((t_vector){0.0F, 0.0F, 0.0F}, OBJ.normal);
	OBJ.cur_mat = OBJ.mats[OBJ.planes[OBJ.cur_plane].shape.material];
}
void	set_val_object(t_env *env, double t, t_ray *ray)
{
	OBJ.new_start = vector_add(ray->start, vector_scale(t, ray->dir));
	OBJ.normal = OBJ.objects[OBJ.cur_object[0]].faces[OBJ.cur_object[1]].normal;
	if (vector_dot(ray->dir, OBJ.normal) > 0.0F)
		OBJ.normal = vector_sub((t_vector){0.0F, 0.0F, 0.0F}, OBJ.normal);
	OBJ.cur_mat = OBJ.mats[OBJ.objects[OBJ.cur_object[0]].material];
}
void	set_val_tri(t_env *env, double t, t_ray *ray)
{
	OBJ.new_start = vector_add(ray->start, vector_scale(t, ray->dir));
	OBJ.normal = TRI[OBJ.cur_tri].normal;
	if (vector_dot(ray->dir, OBJ.normal) > 0.0F)
		OBJ.normal = vector_sub((t_vector){0.0F, 0.0F, 0.0F}, OBJ.normal);
	OBJ.cur_mat = env->obj.mats[TRI[OBJ.cur_tri].shape.material];
}
Exemple #20
0
Vector vector_clamp(Vector v, float max_length)
{
    float length = vector_length(v);

    return length > max_length
        ? vector_scale(v, max_length / length)
        : v;
}
Exemple #21
0
void	scale(t_vec *centre, t_env *env, float t)
{
	t_vec		scaled_hit;
	t_intersect	sct;

	scaled_hit = vector_scale(t, &env->r.dir);
	sct.intersect = vector_add(&env->r.start, &scaled_hit);
	sct.normal = vector_sub(&sct.intersect, centre);
	sct.norm_check = vector_dot(&sct.normal, &sct.normal);
}
Exemple #22
0
bool plane_intersect(plane_t p, vector_t ray_start, vector_t ray_direction, vector_t* hit)
{
    float denominator = vector_dot(p.normal, ray_direction);
    if (denominator == 0)
        return false;
    float t = vector_dot(p.normal, vector_sub(p.point, ray_start)) / denominator;
    if (t < 0.0)
        return false;
    *hit = vector_add(ray_start, vector_scale(ray_direction, t));
    return true;
}
Exemple #23
0
void	lambert_diffusion(t_intersect *sct, t_env *env, float t, t_vec *dist)
{
	t_ray	light_ray;
	float	lambert;

	light_ray.dir = vector_scale((1 / t), dist);
	lambert = vector_dot(&light_ray.dir, &sct->normal) * env->coef;
	env->c.r += lambert * env->l[2]->intensity.r * env->c.r;
	env->c.g += lambert * env->l[2]->intensity.g * env->c.g;
	env->c.b += lambert * env->l[2]->intensity.b * env->c.b;
}
Exemple #24
0
static void update_shape(jigglystruct *js)
{
    vertex *vtx = js->shape->vertices;
    edge *e = js->shape->edges;
    vector zero;

    vector_init(zero, 0, 0, 0);

    /* sum all the vertex-vertex forces */
    while(e) {
        vector f;
        coord mag;
        vector_sub(e->left->vtx->v, e->right->vtx->v, f);
        mag = js->stable_distance - magnitude(f);
        vector_scale(f, mag);
        vector_add_to(e->left->vtx->f, f);
        vector_sub(zero, f, f);
        vector_add_to(e->right->vtx->f, f);
        e = e->next;
    }

    /* scale back the v-v force and add the spherical force
     * then add the result to the vertex velocity, damping
     * if necessary. Finally, move the vertex */
    while(vtx) {
        coord mag;
        vector to_sphere;
        vector_scale(vtx->f, js->hold_strength);
        vector_copy(to_sphere, vtx->v);
        mag = 1 - magnitude(to_sphere);
        vector_scale(to_sphere, mag * js->spherify_strength);
        vector_add_to(vtx->f, to_sphere);
        vector_add_to(vtx->vel, vtx->f);
        vector_init(vtx->f, 0, 0, 0);
        mag = magnitude2(vtx->vel);
        if(mag > js->damping_velocity)
            vector_scale(vtx->vel, js->damping_factor);
        vector_add_to(vtx->v, vtx->vel);
        vtx = vtx->next;
    }
}
Exemple #25
0
static t_vector		reflect_ray(t_env *env, t_ray *ray)
{
	float		reflect;
	t_vector	tmp;
	t_vector	new_dir;

	ray->start = OBJ.new_start;
	reflect = 2.0f * vector_dot(&ray->dir, &OBJ.normal);
	tmp = vector_scale(reflect, &OBJ.normal);
	ray->dir = vector_sub(&ray->dir, &tmp);
	return (new_dir);
}
void fill_metafield(metaball *balls, const int ball_count){
	int x, y, z, i;

	memset(iso_values,0,sizeof(float)*GRIDSIZE*GRIDSIZE*GRIDSIZE);
	memset(iso_normals,0,sizeof(vector)*GRIDSIZE*GRIDSIZE*GRIDSIZE);

	for(i=0;i<ball_count;i++){

		float temp;
		int xstart, xend;
		int ystart, yend;
		int zstart, zend;
		float start;

		balls[i].rr = balls[i].r*balls[i].r;
		balls[i].irr = 1.f/balls[i].rr;

		start = (0.707f*balls[i].r)*GRIDSIZE;

		temp = (balls[i].pos.x*GRIDSIZE)+(GRIDSIZE/2);
		xstart = (int)ceil(temp-start);
		xend = (int)floor(temp+start);
		if(xstart<0) xstart = 0;
		if(xend>GRIDSIZE) xend = GRIDSIZE;

		temp = (balls[i].pos.y*GRIDSIZE)+(GRIDSIZE/2);
		ystart = (int)ceil(temp-start);
		yend = (int)floor(temp+start);
		if(ystart<0) ystart = 0;
		if(yend>GRIDSIZE) yend = GRIDSIZE;

		temp = (balls[i].pos.z*GRIDSIZE)+(GRIDSIZE/2);
		zstart = (int)ceil(temp-start);
		zend = (int)floor(temp+start);
		if(zstart<0) zstart = 0;
		if(yend>GRIDSIZE) yend = GRIDSIZE;


		for(z=zstart;z<zend;z++){
			for(y=ystart;y<yend;y++){
				for(x=xstart;x<xend;x++){
					const vector dir = vector_sub(balls[i].pos,iso_positions[x][y][z]);
					const float rr = ((dir.x*dir.x)+(dir.y*dir.y)+(dir.z*dir.z))*balls[i].irr;
					if(rr<0.5f){
						const float f = (1.f-(rr-rr*rr)*4)*2;
						iso_normals[x][y][z] = vector_add(iso_normals[x][y][z],vector_scale(dir,f));
						iso_values[x][y][z] += f;
					}
				}
			}
		}
	}
}
Exemple #27
0
void	set_val_cone(t_env *env, float t, t_ray ray)
{
	t_vector	scaled;

	scaled = vector_scale(t, &ray.dir);
	OBJ.new_start = vector_add(&ray.start, &scaled);
	OBJ.normal = vector_sub(&OBJ.new_start, &CN_POS(OBJ.cur_cone));
	unrotate_vec2(env, OBJ.cur_cone, &OBJ.normal);
	OBJ.normal.y *= -1.0f;
	rotate_vec_x(CONES[OBJ.cur_cone].rot.x, &OBJ.normal);
	rotate_vec_y(CONES[OBJ.cur_cone].rot.y, &OBJ.normal);
	rotate_vec_z(CONES[OBJ.cur_cone].rot.z, &OBJ.normal);
	if (vector_dot(&OBJ.normal, &OBJ.normal) == 0)
	{
		env->br = 1;
		return ;
	}
	OBJ.normal = vector_scale(1.0f / ABSV(OBJ.normal), &OBJ.normal);
	vector_norm(&OBJ.normal);
	OBJ.cur_mat = env->obj.mats[CONES[OBJ.cur_cone].shape.material];
}
void rWeaponRaybeam::animate(float spf) {

    triggereded = triggered;
    if (trigger) fire();
    trigger = false;

    timeReloading -= spf;
    if (timeReloading < 0) {
        timeReloading = 0.0f;
        if (remainingAmmo == 0 && remainingClips != 0) {
            remainingAmmo = clipSize;
            if (remainingClips > 0) remainingClips--;
        }
    }

    timeFiring -= spf;
    if (timeFiring < 0) {
        timeFiring = 0.0f;
        return;
    }

    float* source = weaponPosef;

    // Vector that points to the Mountpoint relative to the eye.
    float* mpnt = &source[12];
    //vector_print(mpnt);

    // Mountpoint-Forward Vector relative to the eye.
    float* source_8 = &source[8];
    float mfwd[3];
    vector_cpy(mfwd, source_8);
    //vector_print(mfwd);
    vector_norm(mfwd, mfwd);

    // Interpolate single points towards the "tip" of the ray.
    float offset = 80 * (1.0f - (timeFiring / 0.7));
    float worldpos[] = {0, 0, 0};
    vector_scale(worldpos, mfwd, -offset);
    vector_add(worldpos, worldpos, mpnt);
    //vector_print(worldpos);

    // Collide and Damage with single point on ray.
    {
        float radius = 0.5;
        int roles = 0;
        float damage = 25;
        int damaged = damageByParticle(worldpos, radius, roles, damage);
        // Stop ray on collision.
        if (damaged > 0) timeFiring = 0;
    }
}
Exemple #29
0
vector3d ahrs_drift_correction(dataexchange_t *data) {
	vector3d corr_vector, acc_g;

	corr_vector.x = 0.0;
	corr_vector.y = 0.0;
	corr_vector.z = 0.0;

	//do correction only then acceleration is close to 1G (unreliable if greater)
	acc_g = adxl345_raw_to_g(data, SCALE_2G_10B);
	double acc_magnitude = vector_magnitude(acc_g);
	if (fabs(acc_magnitude - 1.0) <= 0.15) {
		float corr_strength = 0.15;

		//vectors for rotation matrix
		vector3d acc_v, mag_v;

		acc_v.x = (*data).acc_x;
		acc_v.y = (*data).acc_y;
		acc_v.z = (*data).acc_z;

		mag_v.x = (*data).mag_x;
		mag_v.y = (*data).mag_y;
		mag_v.z = (*data).mag_z;

		hmc5883l_applyCalibration(&mag_v, calib_ptr);

		vector3d down = vector_inv(acc_v);
		vector3d east = vector_cross(down, mag_v);
		vector3d north = vector_cross(east, down);

		//normalize vectors
		vector_norm(&down);
		vector_norm(&east);
		vector_norm(&north);

		//matrix from rotation quaternion
		matrix3x3d rot_matrix = quaternion_to_matrix((*data).qr);

		//correction vector calculation
		vector3d sum1 = vector_sum(vector_cross(north, matrix_row_to_vector(&rot_matrix, 1)),
				vector_cross(east, matrix_row_to_vector(&rot_matrix, 2)));

		vector3d sum2 = vector_sum(sum1, vector_cross(down, matrix_row_to_vector(&rot_matrix, 3)));

		corr_vector = vector_scale(sum2, corr_strength);
	}

	return corr_vector;
}
void constr_scale_in_direction(float A[3], float B[3], float C[3], float D[3],
			  float E[3], float F[3], float fixed_point[3], 
			  int g)
      /*  scales group by |AB|/|CD| in direction EF */
{
  double lAB,lCD,s;
  float AB[3], CD[3], EF[3], v[3], tmp[3];
  double sp;
  int i;

  if(vector_eq(E,F))
    {
      printf("scaling in direction EF refused: EF = 0 !!!\n");
      return;
    }

  vector_sub(B, A, AB);
  vector_sub(D, C, CD);
  vector_sub(F, E, EF);
  vector_normalize(EF);

  lAB=vector_length(AB);
  lCD=vector_length(CD);
  if(lCD==0) 
    {
      printf("scaling refused: |CD| = 0 !!!\n");
      return;
    }
  s=lAB/lCD;

  if(s== 1.0)
    {
      printf("scaling by 1 does not change anything !!!\n");
      return;
    }

  backup();  
  for(i=0; i<VERTEX_MAX; i++)
    if(vertex_used[i] && group[i]==g)
      {
	vector_sub(vertex[i], fixed_point, v);
	sp=scalar_product(EF,v);
	vectorcpy(tmp, EF);
	vector_scale((s-1)*sp, tmp);
	vector_add(v,tmp, v);
	vector_add(v, fixed_point, vertex[i]);
      }

}