Example #1
0
/*! The IntersectAction callback for Geometry. It computes if the ray used in
    the IntersectAction \a action hits this object and if that is the case,
    which triangle is hit.

    \param[in] action IntersectAction performing the intersect test.
    \return Action result code, \see OSG::Action.

    \note This method is registered with the IntersectAction and automatically
    called from there, you probably never have to call it manually.
*/
Action::ResultE Geometry::intersect(Action * action)
{
    IntersectAction      *ia = dynamic_cast<IntersectAction*>(action);
   
    ia->getActNode()->updateVolume();
    const BoxVolume      &bv = ia->getActNode()->getVolume();

    if(bv.isValid() && !bv.intersect(ia->getLine()))
    {
        return Action::Skip; //bv missed -> can not hit children
    }

    TriangleIterator it  = this->beginTriangles();
    TriangleIterator end = this->endTriangles  ();
    Real32           t;
    Vec3f            norm;
    Line             ia_line(ia->getLine());

    for(; it != end; ++it)
    {
        if(ia_line.intersect(it.getPosition(0),
                             it.getPosition(1),
                             it.getPosition(2), t, &norm))
        {
            ia->setHit(t, ia->getActNode(), it.getIndex(), norm, -1);
        }
    }

    // If we need to test lines, iterate over lines and test for
    // lines that are within width distance from the line
    if(ia->getTestLines())
    {       
       Real32 range_sq  = ia->getTestLineWidth();
       range_sq         = range_sq * range_sq;
       LineIterator it  = this->beginLines();
       LineIterator end = this->endLines  ();
       Pnt3f  pt1, pt2;
       OSG::Vec3f  norm;       

       // Find closest points and if they are within the range, then add a hit
       for(; it != end; ++it)
       {          
          Line cur_line(it.getPosition(0), it.getPosition(1));
          ia_line.getClosestPoints(cur_line, pt1, pt2);
          Real32 dist_sq( pt1.dist2(pt2) );

          if (dist_sq <= range_sq)
          {
             t = ia_line.getPosition().dist(pt1);
             ia->setHit(t, ia->getActNode(), -1, norm, it.getIndex());
          }
       }
    }

    return Action::Continue;
}
Example #2
0
void skip_letter_in_line(struct segm  *segm_ptr,int16_t x)
/*
    This procedure sets up to SPQ.ns_symb adrress of letter
    with given x-coordinate in line.
    segm_ptr - adrress of some segment in line.
    x - x-coordinate.
    SPQ.ns_segm will be address of segment containing *SPQ.ns_symb.
    SPQ.skip_line_x - actual x-coordinate.
*/
 {
  int16_t i;
  int16_t max;

  uchar  *symb,  *c;
  struct segm  *segm_c;
  SPQ.end_l=NO;
  segm_ptr=cur_line(FRAGM,segm_ptr);
  symb=segm_ptr->string;
  i=1;
  if (*symb < 20)
    i=0;
  if (x == LAST_LETTER)
    max=MAX_LINE_LTH;
  else
    max=x;
  for (; i <= max; i++)
   {
    segm_c=segm_ptr;
    c=symb;
    symb=next_symb(YES,YES,YES,segm_ptr,symb);
    segm_ptr=SPQ.ns_segm;
    if (symb == NULL)
     {
      if (x == LAST_LETTER)
        if (c != NULL && *c > 20)
         {
          symb=c;
          SPQ.ns_segm=segm_c;
         }
      else
       {
        if (c != NULL)
          symb=c+define_lth(c);
       }
      SPQ.end_l=YES;
      break;
     }
   }
  SPQ.ns_symb=symb;
  SPQ.skip_line_x=i-1;
  if (SPQ.skip_line_x < 0)
    SPQ.skip_line_x=0;
 }
Example #3
0
/*
    This procedure gives address of first segment in current line.
*/
struct segm  *cur_line(char tt, struct segm * cur_segm)
 {
  if (tt == FRAGM)
   {                                   /* fragm */
    if (cur_segm->prev_in_fragm == NULL)
      return(cur_segm);
    if (cur_segm->type == NEW_LINE)
      return(cur_segm);
    else
      cur_segm=cur_line(FRAGM,cur_segm->prev_in_fragm);
   }
  else
   {                                   /* sheet */
    if (cur_segm->prev_in_sheet == NULL)
      return(cur_segm);
    if (cur_segm->type == NEW_LINE)
      return(cur_segm);
    else
      cur_segm=cur_line(SHEET,cur_segm->prev_in_sheet);
   }
 return(cur_segm);
 }
Example #4
0
bool GBDT::LoadModelFromString(const char* buffer, size_t len) {
  // use serialized string to restore this object
  models_.clear();
  auto c_str = buffer;
  auto p = c_str;
  auto end = p + len;
  std::unordered_map<std::string, std::string> key_vals;
  while (p < end) {
    auto line_len = Common::GetLine(p);
    std::string cur_line(p, line_len);
    if (line_len > 0) {
      if (!Common::StartsWith(cur_line, "Tree=")) {
        auto strs = Common::Split(cur_line.c_str(), '=');
        if (strs.size() == 1) {
          key_vals[strs[0]] = "";
        }
        else if (strs.size() == 2) {
          key_vals[strs[0]] = strs[1];
        }
        else if (strs.size() > 2) {
          if (strs[0] == "feature_names") {
            key_vals[strs[0]] = cur_line.substr(std::strlen("feature_names="));
          } else {
            // Use first 128 chars to avoid exceed the message buffer.
            Log::Fatal("Wrong line at model file: %s", cur_line.substr(0, std::min<size_t>(128, cur_line.size())).c_str());
          }
        }
      }
      else {
        break;
      }
    }
    p += line_len;
    p = Common::SkipNewLine(p);
  }

  // get number of classes
  if (key_vals.count("num_class")) {
    Common::Atoi(key_vals["num_class"].c_str(), &num_class_);
  } else {
    Log::Fatal("Model file doesn't specify the number of classes");
    return false;
  }

  if (key_vals.count("num_tree_per_iteration")) {
    Common::Atoi(key_vals["num_tree_per_iteration"].c_str(), &num_tree_per_iteration_);
  } else {
    num_tree_per_iteration_ = num_class_;
  }

  // get index of label
  if (key_vals.count("label_index")) {
    Common::Atoi(key_vals["label_index"].c_str(), &label_idx_);
  } else {
    Log::Fatal("Model file doesn't specify the label index");
    return false;
  }

  // get max_feature_idx first
  if (key_vals.count("max_feature_idx")) {
    Common::Atoi(key_vals["max_feature_idx"].c_str(), &max_feature_idx_);
  } else {
    Log::Fatal("Model file doesn't specify max_feature_idx");
    return false;
  }

  // get average_output
  if (key_vals.count("average_output")) {
    average_output_ = true;
  }

  // get feature names
  if (key_vals.count("feature_names")) {
    feature_names_ = Common::Split(key_vals["feature_names"].c_str(), ' ');
    if (feature_names_.size() != static_cast<size_t>(max_feature_idx_ + 1)) {
      Log::Fatal("Wrong size of feature_names");
      return false;
    }
  } else {
    Log::Fatal("Model file doesn't contain feature_names");
    return false;
  }

  if (key_vals.count("feature_infos")) {
    feature_infos_ = Common::Split(key_vals["feature_infos"].c_str(), ' ');
    if (feature_infos_.size() != static_cast<size_t>(max_feature_idx_ + 1)) {
      Log::Fatal("Wrong size of feature_infos");
      return false;
    }
  } else {
    Log::Fatal("Model file doesn't contain feature_infos");
    return false;
  }

  if (key_vals.count("objective")) {
    auto str = key_vals["objective"];
    loaded_objective_.reset(ObjectiveFunction::CreateObjectiveFunction(str));
    objective_function_ = loaded_objective_.get();
  }
  if (!key_vals.count("tree_sizes")) {
    while (p < end) {
      auto line_len = Common::GetLine(p);
      std::string cur_line(p, line_len);
      if (line_len > 0) {
        if (Common::StartsWith(cur_line, "Tree=")) {
          p += line_len;
          p = Common::SkipNewLine(p);
          size_t used_len = 0;
          models_.emplace_back(new Tree(p, &used_len));
          p += used_len;
        }
        else {
          break;
        }
      }
      p = Common::SkipNewLine(p);
    }
  } else {
    std::vector<size_t> tree_sizes = Common::StringToArray<size_t>(key_vals["tree_sizes"].c_str(), ' ');
    std::vector<size_t> tree_boundries(tree_sizes.size() + 1, 0);
    int num_trees = static_cast<int>(tree_sizes.size());
    for (int i = 0; i < num_trees; ++i) {
      tree_boundries[i + 1] = tree_boundries[i] + tree_sizes[i];
      models_.emplace_back(nullptr);
    }
    OMP_INIT_EX();
    #pragma omp parallel for schedule(static)
    for (int i = 0; i < num_trees; ++i) {
      OMP_LOOP_EX_BEGIN();
      auto cur_p = p + tree_boundries[i];
      auto line_len = Common::GetLine(cur_p);
      std::string cur_line(cur_p, line_len);
      if (Common::StartsWith(cur_line, "Tree=")) {
        cur_p += line_len;
        cur_p = Common::SkipNewLine(cur_p);
        size_t used_len = 0;
        models_[i].reset(new Tree(cur_p, &used_len));
      } else {
        Log::Fatal("Model format error, expect a tree here. met %s", cur_line.c_str());
      }
      OMP_LOOP_EX_END();
    }
    OMP_THROW_EX();
  }
  num_iteration_for_pred_ = static_cast<int>(models_.size()) / num_tree_per_iteration_;
  num_init_iteration_ = num_iteration_for_pred_;
  iter_ = 0;

  return true;
}
Example #5
0
// Load and release Scripts
HrScriptClass * HrScriptSystemClass::Load_Script(const char * filename)
{
	printf("\nLoading script %s\n",filename);

	int ignored_lines = 0;
	std::ifstream file(filename);
	if (file.is_open() == false)
	{
		printf("Could not find file: %s",filename);
		return NULL;
	}

	// Create a new script to hold the commands
	HrScriptClass * new_script = new HrScriptClass();
	new_script->Set_Name(filename);	

	while (!file.eof())
	{
		std::string line_string;
		std::string cmd_string;
	
		getline (file,line_string);
		//file.ignore(100,'\n');
		std::istringstream cur_line(line_string);
		
		// first, read the "command" 
		cur_line >> cmd_string;
	
		if ((cmd_string.length() >= 2) && (cmd_string[0] == '/') && (cmd_string[1] == '/'))
		{
			printf("Ignoring comment line: %s\n",line_string.c_str());
			ignored_lines++;
		}
		else
		{
			// start setting up this command
			HrScriptCommandClass * new_cmd = Load_Command(cmd_string);

			// if we got a good command type
			if (new_cmd != NULL)
			{
				// Read params to the end of the line:
				int cur_param = 0;
				float param_value = 0.0f;

				while (!cur_line.eof() && (cur_param < new_cmd->Get_Parameter_Count()))
				{
					cur_line >> param_value;
					new_cmd->Set_Parameter(cur_param,param_value);
					cur_param++;
				}
			
				// add the command to the array	
				new_script->Add_Command(new_cmd);

				// Debug display all params
				printf("Found Command: %s ",new_cmd->Get_Command_Name());
				for (int j=0; j<new_cmd->Get_Parameter_Count(); ++j)
				{
					printf(" %f ",new_cmd->Get_Parameter(j));
				}
				printf("\n");
			}
			else
			{
				if (cmd_string.length() > 0)
				{
					printf(" Unrecognized Command: %s\n",cmd_string.c_str());
				}
				ignored_lines++;
			}
		}