Exemple #1
0
static void cross(vertex_t *a, const vertex_t *b,
		  vertex_t *c, const vertex_t *d,
		  double a1,
		  double a2,
		  double a3,
		  double a4,
		  int64_t *s)
{
  float
    r1 = a1 / ((float)a1 + a2),
    r2 = a3 / ((float)a3 + a4);

  ipoint_t pA = {
    a->ip.x + r1 * (b->ip.x - a->ip.x),
    a->ip.y + r1 * (b->ip.y - a->ip.y)
  };
  contrib(pA, b->ip, 1, s);

  ipoint_t pB = {
    c->ip.x + r2 * (d->ip.x - c->ip.x),
    c->ip.y + r2 * (d->ip.y - c->ip.y)
  };
  contrib(d->ip, pB, 1, s);

  ++a->in;
  --c->in;
}
Exemple #2
0
static void inness(const vertex_t *P, size_t cP,
		   const vertex_t *Q, size_t cQ,
		   int64_t *s)
{
  int16_t S = 0;
  size_t c = cQ;

  while (c--)
    {
      ipoint_t p = P[0].ip;

      if ((Q[c].rx.mn < p.x) && (p.x < Q[c].rx.mx))
	{
	  bool positive = (0 < area(p, Q[c].ip, Q[c + 1].ip));

	  if (positive == (Q[c].ip.x < Q[c + 1].ip.x))
	    S += (positive ? -1 : 1);
	}
    }

  for (size_t j = 0 ; j < cP ; ++j)
    {
      if (S)
	contrib(P[j].ip, P[j + 1].ip, S, s);
      S += P[j].in;
    }
}
Exemple #3
0
Float
simplex_noise<Float,N>::sample(
	vector const &in)
{
	Float res = static_cast<Float>(0);
	vector tmp = stretch_m() * in;
	for(typename vector::iterator it = tmp.begin(); it != tmp.end(); ++it)
	{
		*it = std::floor(*it);
	}
	vector floored(tmp);
	tmp = inv_m() * tmp;
	tmp = in - tmp;
	vector offset(tmp);
	tmp = stretch_m() * tmp;

	corner_array c = corners(tmp);
	for (typename corner_array::const_iterator v = c.begin(); v != c.end(); ++v)
	{
		vector t(in - inv_m() * (floored + *v));
		res +=
			contrib(t, floored + *v);
	}

	// FIXME: replace this magic number with something sensible
	return static_cast<Float>(40.0) * res;
}
anneal_puncturing::anneal_puncturing(const char *fname, const int tau,
      const int s)
   {
   // store user parameters
   anneal_puncturing::tau = tau;
   anneal_puncturing::s = s;
   // initialise contribution matrix and load contribution matrix from file
   contrib.init(s, tau, tau);
   FILE *file = fopen(fname, "rb");
   if (file == NULL)
      {
      std::cerr
            << "FATAL ERROR (anneal_puncturing): Cannot open contribution file ("
            << fname << ")." << std::endl;
      exit(1);
      }
   for (int i = 0; i < tau; i++)
      for (int j = 0; j < s; j++)
         for (int k = 0; k < tau; k++)
            {
            double temp;
            if (fscanf(file, "%lf", &temp) == 0)
               assertalways(fscanf(file, "%*[^\n]\n") == 0);
            contrib(j, i, k) = temp;
            }
   fclose(file);
   // initialise the puncturing pattern as odd-even (and transmitting all data bits)
      {
      pattern.init(s, tau);
      for (int i = 0; i < s; i++)
         for (int j = 0; j < tau; j++)
            pattern(i, j) = (i == 0 || (i - 1) % 2 == j % 2);
      }
   // initialise the working vectors
   res.init(tau);
   res = 0;
   // now work the energy
      {
      for (int i = 0; i < s; i++)
         for (int j = 0; j < tau; j++)
            if (!pattern(i, j))
               energy_function(1, i, j);
      }
   // work out the system's initial energy
   E = work_energy();
   }
glm::vec3 trace_recursive_with_lens(RenderData & data, Ray const& ray, int depth)
{
    glm::vec3 contrib(0.0f, 0.0f, 0.0f);

	if (data.context.params.dof && data.context.params.dof_rays > 0)
	{
		// TODO DOF: compute point on focus plane that is common to all rays of this pixel
        float focal_len = data.context.params.focal_length;
		glm::vec3 camDirection = data.context.scene->camera->get_direction();
        float t = focal_len / glm::dot(camDirection, ray.direction);
		
		glm::vec3 focus = glm::vec3(ray.origin + t * ray.direction);

		// TODO DOF: sample random points on the lens
		glm::mat4 ivm = data.context.scene->camera->get_inverse_view_matrix(data.camera_mode);

        for (int i = 0; i < data.context.params.dof_rays; i++)
        {
            glm::vec2 p = uniform_sample_disk(data.tld->rand(), data.tld->rand());
            p = p * data.context.params.lens_radius;

			// TODO DOF: generate ray from the sampled point on the
			// lens through the common point in the focus plane
            glm::vec4 p_world = ivm * glm::vec4(p.x, p.y, 0.f, 0.f);
            glm::vec3 pos = glm::vec3(p_world.x, p_world.y, p_world.z);

            pos += ray.origin;

            glm::vec3 dir = glm::normalize(focus - pos);
            Ray dofRay = Ray(pos, dir);
			// TODO DOF: start ray tracing with the new lens ray
            contrib += trace_recursive(data, dofRay, depth);
		}
		// TODO DOF: compute average contribution of all lens rays
        contrib = contrib / ((float)data.context.params.dof_rays);
	}
	else
        contrib = trace_recursive(data, ray, depth);

    return contrib;
}
void LinearConstraintBC :: assemble(SparseMtrx *answer, TimeStep *tStep, EquationID eid,
                                    CharType type, const UnknownNumberingScheme &r_s,
                                    const UnknownNumberingScheme &c_s)
{


    int size = this->weights.giveSize();
    IntArray lambdaeq(1);
    FloatMatrix contrib(size, 1), contribt;
    IntArray locr(size), locc(size);

    if (!this->lhsType.contains((int) type)) return ;
    this->giveLocArray(r_s, locr, lambdaeq.at(1));

    if (this->isImposed(tStep)) {

      for ( int _i = 1; _i <= size; _i++ ) { // loop over dofs
        double factor=1.;
        if(weightsLtf.giveSize()){
            factor = domain->giveLoadTimeFunction(weightsLtf.at(_i))->__at(tStep->giveIntrinsicTime());
        }
        contrib.at(_i, 1) = this->weights.at(_i)*factor;
      }
      contribt.beTranspositionOf(contrib);

      answer->assemble(lambdaeq, locr, contribt);
      answer->assemble(locr, lambdaeq, contrib);
    } else {
      // the bc is not imposed at specific time step, however in order to make the equation system regular
      // we initialize the allocated equation to the following form 1*labmda = 0, forcing lagrange multiplier 
      // of inactive condition to be zero.
      FloatMatrix help(1,1);
      help.at(1,1) = 1.0;
      answer->assemble(lambdaeq, lambdaeq, help);
    }
}
Exemple #7
0
std::array<unsigned long,vcp<4,1,1>::element_count()> const vcp<4,1,1>::generate_vector( const_vertex_iterator v1, const_vertex_iterator v2 ) {
	std::array<unsigned long,element_count()> counts = {{0}};
	
	std::size_t v1v2( V1V2 * OUT * g.out_edge_exists( v1, v2 ) + V1V2 * IN * g.in_edge_exists( v1, v2 ) );
	
	unsigned long connections( 0 );
	unsigned long amutuals( 0 );
	unsigned long gaps( 0 );
	
	// compose ordered list of v3 candidates
	const_edge_iterator v1_out_neighbors_it( g.out_neighbors_begin( v1 ) );
	const_edge_iterator v1_out_neighbors_end( g.out_neighbors_end( v1 ) );
	const_edge_iterator v1_in_neighbors_it( g.in_neighbors_begin( v1 ) );
	const_edge_iterator v1_in_neighbors_end( g.in_neighbors_end( v1 ) );
	const_edge_iterator v2_out_neighbors_it( g.out_neighbors_begin( v2 ) );
	const_edge_iterator v2_out_neighbors_end( g.out_neighbors_end( v2 ) );
	const_edge_iterator v2_in_neighbors_it( g.in_neighbors_begin( v2 ) );
	const_edge_iterator v2_in_neighbors_end( g.in_neighbors_end( v2 ) );
	assert( MAX_NEIGHBORS > (v1_out_neighbors_end-v1_out_neighbors_it)+(v1_in_neighbors_end-v1_in_neighbors_it)+(v2_out_neighbors_end-v2_out_neighbors_it)+(v2_in_neighbors_end-v2_in_neighbors_it) );
	std::pair<const_vertex_iterator,unsigned short>* v3Vertices_begin( &v3Vertices[0] );
	std::pair<const_vertex_iterator,unsigned short>* v3Vertices_end( &v3Vertices[0] );
	std::pair<const_edge_iterator,directedness_value> min1( next_union_element( v1_out_neighbors_it, v1_out_neighbors_end, v1_in_neighbors_it, v1_in_neighbors_end ) );
	std::pair<const_edge_iterator,directedness_value> min2( next_union_element( v2_out_neighbors_it, v2_out_neighbors_end, v2_in_neighbors_it, v2_in_neighbors_end ) );
	while( min1.first != v1_in_neighbors_end && min2.first != v2_in_neighbors_end ) {
		if( g.target_of( min1.first ) < g.target_of( min2.first ) ) {
			if( g.target_of( min1.first ) != v2 ) {
				++connections;
				++gaps;
				if( min1.second < 3 ) {
					++amutuals;
				}
				v3Vertices_end->first = g.target_of( min1.first );
				v3Vertices_end->second = v1v2 + V1V3 * min1.second;
				++v3Vertices_end;
			}
			min1 = next_union_element( v1_out_neighbors_it, v1_out_neighbors_end, v1_in_neighbors_it, v1_in_neighbors_end );
		} else if( g.target_of( min1.first ) > g.target_of( min2.first ) ) {
			if( g.target_of( min2.first ) != v1 ) {
				++connections;
				++gaps;
				if( min2.second < 3 ) {
					++amutuals;
				}
				v3Vertices_end->first = g.target_of( min2.first );
				v3Vertices_end->second = v1v2 + V2V3 * min2.second;
				++v3Vertices_end;
			}
			min2 = next_union_element( v2_out_neighbors_it, v2_out_neighbors_end, v2_in_neighbors_it, v2_in_neighbors_end );
		} else { // the next neighbor is shared by both v1 and v2, so it cannot be either and we do not need to check to exclude it
			connections += 2;
			if( min1.second < 3 ) {
				++amutuals;
			}
			if( min2.second < 3 ) {
				++amutuals;
			}
		 	v3Vertices_end->first = g.target_of( min1.first );
		 	v3Vertices_end->second = v1v2 + V1V3 * min1.second + V2V3 * min2.second;
			++v3Vertices_end;
			min1 = next_union_element( v1_out_neighbors_it, v1_out_neighbors_end, v1_in_neighbors_it, v1_in_neighbors_end );
			min2 = next_union_element( v2_out_neighbors_it, v2_out_neighbors_end, v2_in_neighbors_it, v2_in_neighbors_end );
		}
	}
	while( min1.first != v1_in_neighbors_end ) {
		if( g.target_of( min1.first ) != v2 ) {
			++connections;
			++gaps;
			if( min1.second < 3 ) {
				++amutuals;
			}
			v3Vertices_end->first = g.target_of( min1.first );
			v3Vertices_end->second = v1v2 + V1V3 * min1.second;
			++v3Vertices_end;
		}
		min1 = next_union_element( v1_out_neighbors_it, v1_out_neighbors_end, v1_in_neighbors_it, v1_in_neighbors_end );
	}
	while( min2.first != v2_in_neighbors_end ) {
		if( g.target_of( min2.first ) != v1 ) {
			++connections;
			++gaps;
			if( min2.second < 3 ) {
				++amutuals;
			}
			v3Vertices_end->first = g.target_of( min2.first );
			v3Vertices_end->second = v1v2 + V2V3 * min2.second;
			++v3Vertices_end;
		}
		min2 = next_union_element( v2_out_neighbors_it, v2_out_neighbors_end, v2_in_neighbors_it, v2_in_neighbors_end );
	}

	unsigned long v3_count( v3Vertices_end-v3Vertices_begin );
	unsigned long v4_count( 0 );
	for( std::pair<const_vertex_iterator,unsigned short>* it1( v3Vertices_begin ); it1 != v3Vertices_end; ++it1 ) { // for each v3 vertex computed above
		const_edge_iterator v3_out_neighbors_it( g.out_neighbors_begin( it1->first ) );
		const_edge_iterator v3_out_neighbors_end( g.out_neighbors_end( it1->first ) );
		const_edge_iterator v3_in_neighbors_it( g.in_neighbors_begin( it1->first ) );
		const_edge_iterator v3_in_neighbors_end( g.in_neighbors_end( it1->first ) );
		unsigned long v4_local_count( 0 ); // keep track of how many v4 vertices are only the result of the neighbors of this v3
		std::pair<const_edge_iterator,directedness_value> min( next_union_element( v3_out_neighbors_it, v3_out_neighbors_end, v3_in_neighbors_it, v3_in_neighbors_end ) );
		for( std::pair<const_vertex_iterator,unsigned short>* it2( v3Vertices_begin ); it2 != v3Vertices_end; ++it2 ) {	
			while( min.first != v3_in_neighbors_end && g.target_of( min.first ) < it2->first ) {
				if( g.target_of( min.first ) != v1 && g.target_of( min.first ) != v2 ) {
					++v4_local_count;
					if( min.second < 3 ) {
						++amutuals;
					}
					++counts[ element_address( it1->second + V3V4 * min.second ) ];
				}
				min = next_union_element( v3_out_neighbors_it, v3_out_neighbors_end, v3_in_neighbors_it, v3_in_neighbors_end );
			}
			if( min.first == v3_in_neighbors_end || g.target_of( min.first ) > it2->first ) {
				if( it1->first < it2->first ) {
					unsigned short temp( it2->second - v1v2 );
					std::size_t contrib( 0 );
					contrib += V1V4 * (temp % V2V3);
					contrib += V2V4 * (temp / V2V3);
					++gaps;
					++counts[ element_address( it1->second + contrib ) ];
				}
			} else {
				if( it1->first < it2->first ) {
					unsigned short temp( it2->second - v1v2 );
					std::size_t contrib( 0 );
					contrib += V1V4 * (temp % V2V3);
					contrib += V2V4 * (temp / V2V3);
					++connections;
					if( min.second < 3 ) {
						++amutuals;
					}
					++counts[ element_address( it1->second + contrib + V3V4 * min.second  ) ];
				}
				min = next_union_element( v3_out_neighbors_it, v3_out_neighbors_end, v3_in_neighbors_it, v3_in_neighbors_end );
			}
		}
		while( min.first != v3_in_neighbors_end ) {
			if( g.target_of( min.first ) != v1 && g.target_of( min.first ) != v2 ) {
				++v4_local_count;
				if( min.second < 3 ) {
					++amutuals;
				}
				++counts[ element_address( it1->second + V3V4 * min.second ) ];
			}
			min = next_union_element( v3_out_neighbors_it, v3_out_neighbors_end, v3_in_neighbors_it, v3_in_neighbors_end );
		}
		v4_count += v4_local_count;
		connections += v4_local_count;
		gaps += 2*v4_local_count;
		counts[ element_address( it1->second ) ] += g.vertex_count() - 2 - v3_count - v4_local_count;
	}
		
	// account for the least connected substructures
	counts[ element_address( v1v2+OUT*V3V4) ] = this->amutualPairs - (amutuals + static_cast<bool>(v1v2)); // out and in versions are isomorphically equivalent and do not need to be counted separately
	counts[ element_address( v1v2+BOTH*V3V4 ) ] = this->mutualPairs - (connections - amutuals);
	counts[ element_address( v1v2 ) ] = unconnected_pairs - (gaps + !static_cast<bool>(v1v2)) - (2 + v3_count) * (g.vertex_count() - 2 - v3_count) + 3 * v4_count;
	
	return counts;
}
inline void anneal_puncturing::energy_function(const double factor,
      const int set, const int pos)
   {
   for (int i = 0; i < tau; i++)
      res(i) += factor * contrib(set, pos, i);
   }