Beispiel #1
0
void Path::stitch(Sequence::iterator first_replaced,
                  Sequence::iterator last_replaced,
                  Sequence &source)
{
  if (!source.empty()) {
    if ( first_replaced != get_curves().begin() ) {
      if ( (*first_replaced)->initialPoint() != source.front()->initialPoint() ) {
        Curve *stitch = new StitchSegment((*first_replaced)->initialPoint(),
                                          source.front()->initialPoint());
        source.insert(source.begin(), boost::shared_ptr<Curve>(stitch));
      }
    }
    if ( last_replaced != (get_curves().end()-1) ) {
      if ( (*last_replaced)->finalPoint() != source.back()->finalPoint() ) {
        Curve *stitch = new StitchSegment(source.back()->finalPoint(),
                                          (*last_replaced)->finalPoint());
        source.insert(source.end(), boost::shared_ptr<Curve>(stitch));
      }
    }
  } else if ( first_replaced != last_replaced && first_replaced != get_curves().begin() && last_replaced != get_curves().end()-1) {
    if ( (*first_replaced)->initialPoint() != (*(last_replaced-1))->finalPoint() ) {
      Curve *stitch = new StitchSegment((*(last_replaced-1))->finalPoint(),
                                        (*first_replaced)->initialPoint());
      source.insert(source.begin(), boost::shared_ptr<Curve>(stitch));
    }
  }
}
Beispiel #2
0
void Path::do_append(Curve *c) {
  boost::shared_ptr<Curve> curve(c);
  if ( get_curves().front().get() == final_ ) {
    final_->setPoint(1, curve->initialPoint());
  } else {
    if (curve->initialPoint() != finalPoint()) {
      THROW_CONTINUITYERROR();
    }
  }
  get_curves().insert(get_curves().end()-1, curve);
  final_->setPoint(0, curve->finalPoint());
}
Beispiel #3
0
void FacetPoint::get_coedges( DLIList<FacetCoEdge*>& result_list )
{
  DLIList<FacetCurve*> curve_list;
  get_curves( curve_list );
  curve_list.reset();
  for ( int i = curve_list.size(); i--; )
    curve_list.get_and_step()->get_coedges( result_list );
}
Beispiel #4
0
void Path::do_update(Sequence::iterator first_replaced,
                     Sequence::iterator last_replaced,
                     Sequence::iterator first,
                     Sequence::iterator last)
{
  // note: modifies the contents of [first,last)
  check_continuity(first_replaced, last_replaced, first, last);
  if ( ( last - first ) == ( last_replaced - first_replaced ) ) {
    std::copy(first, last, first_replaced);
  } else {
    // this approach depends on std::vector's behavior WRT iterator stability
    get_curves().erase(first_replaced, last_replaced);
    get_curves().insert(first_replaced, first, last);
  }

  if ( get_curves().front().get() != final_ ) {
    final_->setPoint(0, back().finalPoint());
    final_->setPoint(1, front().initialPoint());
  }
}
Beispiel #5
0
Path &Path::operator*=(Affine const &m) {
  unshare();
  Sequence::iterator last = get_curves().end() - 1;
  Sequence::iterator it;
  Point prev;
  for (it = get_curves().begin() ; it != last ; ++it) {
    *it = boost::shared_ptr<Curve>((*it)->transformed(m));
    if ( it != get_curves().begin() && (*it)->initialPoint() != prev ) {
      THROW_CONTINUITYERROR();
    }
    prev = (*it)->finalPoint();
  }
  for ( int i = 0 ; i < 2 ; ++i ) {
    final_->setPoint(i, (*final_)[i] * m);
  }
  if (get_curves().size() > 1) {
    if ( front().initialPoint() != initialPoint() || back().finalPoint() != finalPoint() ) {
      THROW_CONTINUITYERROR();
    }
  }
  return *this;
}
Beispiel #6
0
void Path::check_continuity(Sequence::iterator first_replaced,
                            Sequence::iterator last_replaced,
                            Sequence::iterator first,
                            Sequence::iterator last)
{
  if ( first != last ) {
    if ( first_replaced != get_curves().begin() ) {
      if ( (*first_replaced)->initialPoint() != (*first)->initialPoint() ) {
        THROW_CONTINUITYERROR();
      }
    }
    if ( last_replaced != (get_curves().end()-1) ) {
      if ( (*(last_replaced-1))->finalPoint() != (*(last-1))->finalPoint() ) {
        THROW_CONTINUITYERROR();
      }
    }
  } else if ( first_replaced != last_replaced && first_replaced != get_curves().begin() && last_replaced != get_curves().end()-1) {
    if ( (*first_replaced)->initialPoint() != (*(last_replaced-1))->finalPoint() ) {
      THROW_CONTINUITYERROR();
    }
  }
}
static cv::RotatedRect find_best_edge(cv::Mat *pic,cv::Mat *edge, int start_x, int end_x, int start_y, int end_y, double mean_dist, int inner_color_range){

	cv::RotatedRect ellipse;
	ellipse.center.x=0;
	ellipse.center.y=0;
	ellipse.angle=0.0;
	ellipse.size.height=0.0;
	ellipse.size.width=0.0;

	std::vector<std::vector<cv::Point>> all_curves=get_curves(pic, edge, start_x, end_x, start_y, end_y, mean_dist, inner_color_range);





	if(all_curves.size()==1){
		ellipse=cv::fitEllipse( cv::Mat(all_curves[0]) );

		if(ellipse.center.x<0 || ellipse.center.y<0 || ellipse.center.x>pic->cols || ellipse.center.y>pic->rows){
			ellipse.center.x=0;
			ellipse.center.y=0;
			ellipse.angle=0.0;
			ellipse.size.height=0.0;
			ellipse.size.width=0.0;
		}

	}else{
		ellipse.center.x=0;
		ellipse.center.y=0;
		ellipse.angle=0.0;
		ellipse.size.height=0.0;
		ellipse.size.width=0.0;
	}

	return ellipse;

}
Beispiel #8
0
/* fit potential energy parameters via simulated annealing */
int surface_fit(system_t *system) {

	//override default values if specified in input file
	double temperature    = ((system->fit_start_temp     ) ? system->fit_start_temp     : TEMPERATURE    );
	double max_energy     = ((system->fit_max_energy     ) ? system->fit_max_energy     : MAX_ENERGY     );
	double schedule       = ((system->fit_schedule       ) ? system->fit_schedule       : SCHEDULE       );

	//used only if qshift is on
	double quadrupole=0;

	int i, j,  // generic counters
	nCurves,   // number of curves to fit against
	nPoints,   // number of points in each curve
	nSteps;
	double *r_input=0; // Master list of r-values
	curveData_t *curve=0;   // Array to hold curve point data
	param_t *param_ptr;
	param_g *params;
	double r_min, r_max, r_inc;
	double current_error, last_error, global_minimum;
	qshiftData_t * qshiftData = NULL; //used only with qshift

	// ee_local variables
	int a = 0;
	int globalFound = 0;
	double oldGlobal = 0;
	double initEps[2];
	double initSig[2];
	double globalEps[2];
	double globalSig[2];

	// Record number of curves for convenient reference
	nCurves = system->fit_input_list.data.count;

	// Read the curve data from the input files into a local array
	curve =  readFitInputFiles( system, nCurves );
	if(!curve) {
			error( "SURFACE: An error occurred while reading the surface-fit input files.\n" );
			return (-1);
	}

	// Record number of points in each curve, for convenient reference
	nPoints = curve[0].nPoints;

	// Make a master list of R-values
	r_input = curve[0].r;
	curve[0].r = 0;

	// Free the memory used by all the remaining lists (i.e. all lists
	// other than the one whose address was just transferred).
	for( i=1; i<nCurves; i++ )
			free(curve[i].r);

	// Allocate memory for the output and global arrays
	if ( -1 == alloc_curves ( nCurves, nPoints, curve ) ) {
		free(r_input);
		return -1;
	}

	// Determine Independent Domain
	//////////////////////////////////
	r_min = r_input[0];
	r_max = r_input[nPoints-1];
	r_inc = r_input[1] - r_input[0];

	// Output the geometry for visual verification
	output_pqrs ( system, nCurves, curve );	

	// Get the Initial Error
	//////////////////////////////////

	//obtain curves
	get_curves ( system, nCurves, curve, r_min, r_max, r_inc );

	//calc current error
	current_error = error_calc ( system, nCurves, nPoints, curve, max_energy);

	//record parameters. we will determine which ones need to be adjusted later
	params = record_params ( system );

	// print some header info
	for(param_ptr = params->type_params; param_ptr; param_ptr = param_ptr->next) {
			printf( "SURFACE: Atom type: %d @ %s\n", param_ptr->ntypes, param_ptr->atomtype );
			
			if(param_ptr->epsilon != 0) {
				initEps[a] = param_ptr->epsilon;
				initSig[a] = param_ptr->sigma;
				a++;
			}
	}
	if ( ! system->fit_boltzmann_weight ) 
		printf("*** any input energy values greater than %f K will not contribute to the fit ***\n", max_energy);

	//write params and current_error to stdout
	printf("*** Initial Fit: \n");
	output_params ( temperature, current_error, params );
	printf("*****************\n");

	//set the minimum error we've found
	global_minimum = current_error;
	last_error     = current_error;
	for( j=0; j<nCurves; j++ )
			for(i = 0; i < nPoints; i++)
					curve[j].global[i] = curve[j].output[i];

	//initialize stuff for qshift
	if ( system->surf_qshift_on ) {
		qshiftData = malloc(sizeof(qshiftData_t));
		quadrupole = calcquadrupole(system);
	}

	// ANNEALING
	// Loop over temperature. When the temperature reaches MIN_TEMPERATURE
	// we quit. Assuming we find an error smaller than the initial error
	// we'll spit out a fit_geometry.pqr file, and you'll want to re-anneal.
	for(nSteps = 0; temperature > MIN_TEMPERATURE; ++nSteps) {

		// randomly perturb the parameters
		surf_perturb ( system, quadrupole, qshiftData, params );
	
		// apply the trial parameters
		surface_dimer_parameters ( system, params);
		get_curves ( system, nCurves, curve, r_min, r_max, r_inc );

		// calc error
		current_error = error_calc ( system, nCurves, nPoints, curve, max_energy);

		int condition =  0;
		if (system->surf_descent) condition = current_error < last_error;
		else condition = get_rand() < exp(-(current_error - last_error)/temperature);

 		//  DO MC at this 'temperature'
		if(condition) {
		//ACCEPT MOVE /////
			apply_new_parameters ( params );
			last_error = current_error;

			if(nSteps >= EQUIL) {
				nSteps = 0;

				//write params and current_error to stdout
				output_params ( temperature, current_error, params );

				// output the new global minimum
				if(current_error < global_minimum) {
					system->fit_best_square_error = global_minimum = current_error;
					new_global_min ( system, nCurves, nPoints, curve );

					// Store the LJ parameters corresponding to global min
					a = 0;
					globalFound = 1;
					oldGlobal = current_error;	// keep track for ee_local
					for(param_ptr = params->type_params; param_ptr; param_ptr = param_ptr->next) {
						if(param_ptr->epsilon != 0) {
							globalEps[a] = param_ptr->epsilon;
							globalSig[a] = param_ptr->sigma;
							a++;
						}
					}
				}
			}
		}
		else {
		// MOVE REJECTED ////
				revert_parameters ( system, params );
		} //END DO MONTE CARLO

		// decrement the temperature
		temperature = temperature*schedule;	// schedule
	} //END ANNEALING

	// Output the Fit Curves
	output_fit ( nCurves, nPoints, curve, max_energy, r_input );

	// Exhaustive Enumeration -- Chris Cioce
	if (system->ee_local) {
		int b, c, d, nE1, nE2, nS1, nS2;
		double currEps[2];
		double currSig[2];
		double newGlobal=0;
		double E1start, E1stop, E1incr, E2start, E2stop, E2incr, E1c, E2c; // E1, E2 (unused)
		double S1start, S1stop, S1incr, S2start, S2stop, S2incr, S1c, S2c; // S1, S2 (unused)

		if(globalFound == 1) {
			printf("\nEE_LOCAL ~> SA found an improved surface. Using current global minimum parameters for EE.\n");
			currEps[0] = globalEps[0];
			currEps[1] = globalEps[1];
			currSig[0] = globalSig[0];
			currSig[1] = globalSig[1];
		} else {
			printf("\nEE_LOCAL ~> SA did not find an improved surface. Using initial parameters for EE.\n");
			currEps[0] = initEps[0];
			currEps[1] = initEps[1];
			currSig[0] = initSig[0];
			currSig[1] = initSig[1];
		}

		for(a=0; a<2; a++) {
			printf("EE_LOCAL ~> InitEps[%d]: %f\tInitSig[%d]: %f\tGlobalEps[%d]: %f\tGlobalSig[%d]: %f\n",a,initEps[a],a,initSig[a],a,globalEps[a],a,globalSig[a]);
		}

		E1start = currEps[0] - (currEps[0]*system->range_eps);
		E1stop  = currEps[0] + (currEps[0]*system->range_eps);
		E1incr  = currEps[0] * system->step_eps;
		E2start = currEps[1] - (currEps[1]*system->range_eps);
		E2stop  = currEps[1] + (currEps[1]*system->range_eps);
		E2incr  = currEps[1] * system->step_eps;
		S1start = currSig[0] - (currSig[0]*system->range_sig);
		S1stop  = currSig[0] + (currSig[0]*system->range_sig);
		S1incr  = currSig[0] * system->step_sig;
		S2start = currSig[1] - (currSig[1]*system->range_sig);
		S2stop  = currSig[1] + (currSig[1]*system->range_sig);
		S2incr  = currSig[1] * system->step_sig;
		nE1 = ((E1stop - E1start) / E1incr) + 1;
		nE2 = ((E2stop - E2start) / E2incr) + 1;
		nS1 = ((S1stop - S1start) / S1incr) + 1;
		nS2 = ((S2stop - S2start) / S2incr) + 1;
		printf("EE_LOCAL ~> E1start = %f\tE1stop = %f\tE1incr = %f\tnE1 = %d\n",E1start,E1stop,E1incr,nE1);
		printf("EE_LOCAL ~> E2start = %f\tE2stop = %f\tE2incr = %f\tnE2 = %d\n",E2start,E2stop,E2incr,nE2);
		printf("EE_LOCAL ~> S1start = %f\tS1stop = %f\tS1incr = %f\tnS1 = %d\n",S1start,S1stop,S1incr,nS1);
		printf("EE_LOCAL ~> S2start = %f\tS2stop = %f\tS2incr = %f\tnS2 = %d\n",S2start,S2stop,S2incr,nS2);
		printf("EE_LOCAL ~> Will now perform %d total iterations in parameter subspace...\n",(nE1*nE2*nS1*nS2));

		E1c = E1start;
		E2c = E2start;
		S1c = S1start;
		S2c = S2start;

		// Set initial parameters
		for(param_ptr = params->type_params; param_ptr; param_ptr = param_ptr->next) {
			if(param_ptr->epsilon == currEps[0]) {
				param_ptr->epsilon = E1c;
			}
			if(param_ptr->epsilon == currEps[1]) {
				param_ptr->epsilon = E2c;
			}
			if(param_ptr->sigma == currSig[0]) {
				param_ptr->sigma = S1c;
			}
			if(param_ptr->sigma == currSig[1]) {
				param_ptr->sigma = S2c;
			}
		}

		// apply initial parameters
		surface_dimer_parameters ( system, params);
		get_curves ( system, nCurves, curve, r_min, r_max, r_inc );

		//calc current error
		current_error = error_calc ( system, nCurves, nPoints, curve, max_energy);

		//record parameters. we will determine which ones need to be adjusted later
		//params = record_params ( system );

		//write params and current_error to stdout <-- just for visual verification
		printf("*** Initial Fit: \n");
		output_params ( 0.0, current_error, params );
		printf("*****************\n");

		// Main loops
		globalFound = 0;
		int master = 1;
		for(a=0; a<=nE1; a++) {
			for(b=0; b<=nE2; b++) {
				for(c=0; c<=nS1; c++) {
					for(d=0; d<=nS2; d++) {
						//printf("MASTER: %d\ta: %d\t(%f)\tb: %d\t(%f)\tc: %d\t(%f)\td: %d\t(%f)\n", master, a, E1c, b, E2c, c, S1c, d, S2c);	// Debug output

						// apply parameters
						surface_dimer_parameters ( system, params);
						get_curves ( system, nCurves, curve, r_min, r_max, r_inc );

						// calc current error
						current_error = error_calc ( system, nCurves, nPoints, curve, max_energy);

						//output_params ( 0.0, current_error, params );	// Debug output

						// check for new global minimum
						if(current_error < global_minimum) {
							system->fit_best_square_error = global_minimum = current_error;
							new_global_min ( system, nCurves, nPoints, curve );

							// Store the LJ parameters corresponding to global min
							a = 0;
							globalFound = 1;
							newGlobal = current_error;
							for(param_ptr = params->type_params; param_ptr; param_ptr = param_ptr->next) {
								if(param_ptr->epsilon != 0) {
									globalEps[a] = param_ptr->epsilon;
									globalSig[a] = param_ptr->sigma;
									a++;
								}
							}
						}

						// Set updated S2 parameter
						for(param_ptr = params->type_params; param_ptr; param_ptr = param_ptr->next) {
							if(param_ptr->sigma == S2c) {
								param_ptr->sigma = (S2c+S2incr);
							}
						}
						S2c += S2incr;
						
						master++;
					} // END S2 loop

					// Set updated S1 & S2 parameters
					for(param_ptr = params->type_params; param_ptr; param_ptr = param_ptr->next) {
						if(param_ptr->sigma == S1c) {
							param_ptr->sigma = (S1c+S1incr);
						}
						if(param_ptr->sigma == S2c) {
							param_ptr->sigma = S2start;
						}
					}

					S1c += S1incr;
					S2c = S2start;

				} // END S1 loop

				// Set updated E2 & S1 parameters
				for(param_ptr = params->type_params; param_ptr; param_ptr = param_ptr->next) {
					if(param_ptr->epsilon == E2c) {
						param_ptr->epsilon = (E2c+E2incr);
					}
					if(param_ptr->sigma == S1c) {
						param_ptr->sigma = S1start;
					}
				}

				E2c += E2incr;
				S1c = S1start;

			} // END E2 loop

			// Set updated E1 & E2 parameters
			for(param_ptr = params->type_params; param_ptr; param_ptr = param_ptr->next) {
				if(param_ptr->epsilon == E1c) {
					param_ptr->epsilon = (E1c+E1incr);
				}
				if(param_ptr->epsilon == E2c) {
					param_ptr->epsilon = E2start;
				}
			}

			E1c += E1incr;
			E2c = E2start;

		} // END E1 loop

		// If a new global min is found, write to stdout
		// TODO: Adjust printf statements to prettify final output :D
		if(globalFound == 1) {
			printf("\nEE_LOCAL ~> EE found an improved surface. Parameters written to fit_geometry.pqr\n");
			//printf("EE_LOCAL ~> E1: %f\tS1: %f\tE2: %f\tS2: %f\n", globalEps[0], globalSig[0], globalEps[1], globalSig[1]);
			printf("EE_LOCAL ~> SA Global Min: %f\n", oldGlobal);
			printf("EE_LOCAL ~> EE Global Min: %f\n", newGlobal);
			printf("            ----------------------------\n");
			printf("            ----------------------------\n");
			printf("               ====> %.2f %% improvement\n\n", ((oldGlobal-newGlobal)/oldGlobal)*100);
		} else {
			printf("EE_LOCAL ~> EE was unable to find an improved surface versus SA. Oh well, at least you tried...and for that, we thank you!\n\n");
		}

	} // END ee_local

	// Return all memory back to the system
	free_all_mem (nCurves, curve, params, qshiftData, r_input);
	return(0);
}