Пример #1
0
/**
 * The primitive is not cloned, nor is it unaltered; <b>this</b> points to
 * the pointer returned by this method (typically <b>primitive</b>).
 * \return primitive <i>unless the geometry of the underlying primitive is 
 * inconsistent, degenerate, or non-convex<i>; in that case,  
 * a corrected primitive will be returned.
 */
PrimitivePtr CollisionGeometry::set_geometry(PrimitivePtr primitive)
{
  Quatd EYE;

  if (_single_body.expired())
    throw std::runtime_error("CollisionGeometry::set_geometry() called before single body set!");

  SingleBodyPtr sb(_single_body);
  RigidBodyPtr rb = dynamic_pointer_cast<RigidBody>(sb);
  if (rb && !Quatd::rel_equal(rb->get_pose()->q, EYE))
  {
    std::cerr << "CollisionGeometry::set_primitive() warning - rigid body's orientation is not identity." << std::endl;
    std::cerr << "  At the rigid body's current orientation (" << AAngled(rb->get_pose()->q) << ")" << std::endl;
    std::cerr << "  the primitive will have the orientation (" << AAngled(primitive->get_pose()->q) << ")" << std::endl;
  }

  // save the primitive
  _geometry = primitive;

  // add this to the primitive
  CollisionGeometryPtr cg = dynamic_pointer_cast<CollisionGeometry>(shared_from_this());
  primitive->add_collision_geometry(cg);

  return primitive;
}
Пример #2
0
void IECoreAppleseed::MotionBlockHandler::primitive( PrimitivePtr primitive, const string &materialName )
{
	if( m_blockType == NoBlock )
	{
		assert( m_primitives.empty() );

		m_blockType = PrimitiveBlock;
		m_primitiveType = primitive->typeId();
		m_materialName = materialName;
	}
	else
	{
		if( m_blockType != PrimitiveBlock )
		{
			msg( MessageHandler::Error, "IECoreAppleseed::RendererImplementation::primitive", "Bad call in motion block." );
		}

		if( m_primitiveType != primitive->typeId() )
		{
			msg( MessageHandler::Error, "IECoreAppleseed::RendererImplementation::primitive", "Cannot mix primitive types in motion block." );
		}
	}

	m_primitives.push_back( primitive );
}
Пример #3
0
/// Calculates the signed distance for a primitive
double CollisionGeometry::calc_signed_dist(const Point3d& p)
{
  // get the primitive from this
  PrimitivePtr primitive = get_geometry();

  // get the collision geometry
  CollisionGeometryPtr cg = dynamic_pointer_cast<CollisionGeometry>(shared_from_this());

  // setup a new point with a new pose
  Point3d px = Pose3d::transform_point(primitive->get_pose(cg), p);

  // call the primitive function
  return primitive->calc_signed_dist(px);
}
Пример #4
0
/// Calculates the signed distances between two geometries and returns closest points if geometries are not interpenetrating
double CollisionGeometry::calc_signed_dist(CollisionGeometryPtr gA, CollisionGeometryPtr gB, Point3d& pA, Point3d& pB) 
{
  // get the two primitives
  PrimitivePtr primA = gA->get_geometry();
  PrimitivePtr primB = gB->get_geometry();

  // setup poses for the points
  pA.pose = primA->get_pose(gA);
  pB.pose = primB->get_pose(gB);

  FILE_LOG(LOG_COLDET) << "CollisionGeometry::calc_signed_dist() - computing signed distance between " << gA->get_single_body()->id << " and " << gB->get_single_body()->id << std::endl;

  // now compute the signed distance
  return primA->calc_signed_dist(primB, pA, pB);
}
Пример #5
0
/// Calculates the (unsigned) distance of a point from this collision geometry
double CollisionGeometry::calc_dist_and_normal(const Point3d& p, std::vector<Vector3d>& normals) const
{
  // get the primitive from this
  PrimitivePtr primitive = get_geometry();

  // get the collision geometry
  shared_ptr<const CollisionGeometry> cg_const = dynamic_pointer_cast<const CollisionGeometry>(shared_from_this());
  CollisionGeometryPtr cg = const_pointer_cast<CollisionGeometry>(cg_const);

  // setup a new point with a new pose
  Point3d px = Pose3d::transform_point(primitive->get_pose(cg), p);

  // call the primitive function
  return primitive->calc_dist_and_normal(px, normals);
}
Пример #6
0
/// Gets vertices for a primitive
void CollisionGeometry::get_vertices(std::vector<Point3d>& vertices) const
{
  // get the primitive from this
  PrimitivePtr primitive = get_geometry();

  // get a pointer to this
  shared_ptr<const CollisionGeometry> cg_const = dynamic_pointer_cast<const CollisionGeometry>(shared_from_this());
  CollisionGeometryPtr cg = const_pointer_cast<CollisionGeometry>(cg_const);

  // get the pose for this
  shared_ptr<const Pose3d> P = primitive->get_pose(cg);
 
  // get the vertices from the primitive
  vertices.clear();
  primitive->get_vertices(P, vertices);
}
asf::auto_release_ptr<asr::Object> IECoreAppleseed::BatchPrimitiveConverter::doConvertPrimitive( PrimitivePtr primitive, const string &name )
{
	MurmurHash primitiveHash;
	primitiveHash.append( name );
	primitive->hash( primitiveHash );

	if( primitive->typeId() == MeshPrimitiveTypeId )
	{
		// Check if we already have a mesh saved for this object.
		string fileName = string( "_geometry/" ) + primitiveHash.toString() + m_meshGeomExtension;
		filesystem::path p = m_projectPath / fileName;

		if( !filesystem::exists( p ) )
		{
			ToAppleseedConverterPtr converter = ToAppleseedConverter::create( primitive.get() );

			if( !converter )
			{
				msg( Msg::Warning, "IECoreAppleseed::BatchPrimitiveConverter", "Couldn't convert primitive." );
				return asf::auto_release_ptr<asr::Object>();
			}

			asf::auto_release_ptr<asr::Object> entity;
			entity.reset( static_cast<asr::Object*>( converter->convert() ) );

			if( entity.get() == 0 )
			{
				msg( Msg::Warning, "IECoreAppleseed::BatchPrimitiveConverter", "Couldn't convert primitive." );
				return asf::auto_release_ptr<asr::Object>();
			}

			// Write the mesh to a file.
			p = m_projectPath / fileName;
			if( !asr::MeshObjectWriter::write( static_cast<const asr::MeshObject&>( *entity ), name.c_str(), p.string().c_str() ) )
			{
				msg( Msg::Warning, "IECoreAppleseed::BatchPrimitiveConverter", "Couldn't save mesh primitive." );
				return asf::auto_release_ptr<asr::Object>();
			}
		}

		asf::auto_release_ptr<asr::MeshObject> meshObj( asr::MeshObjectFactory().create( name.c_str(), asr::ParamArray().insert( "filename", fileName.c_str() ) ) );
		return asf::auto_release_ptr<asr::Object>( meshObj.release() );
	}

	return asf::auto_release_ptr<asr::Object>();
}
Пример #8
0
/// Gets a supporting point for this geometry in a particular direction
Point3d CollisionGeometry::get_supporting_point(const Vector3d& d) const
{
  // get the primitive from this
  PrimitivePtr primitive = get_geometry();

  // get this
  shared_ptr<const CollisionGeometry> cg_const = dynamic_pointer_cast<const CollisionGeometry>(shared_from_this());
  CollisionGeometryPtr cg = const_pointer_cast<CollisionGeometry>(cg_const);

  // get the pose for this
  shared_ptr<const Pose3d> P = primitive->get_pose(cg);

  // transform the vector
  Vector3d dir = Pose3d::transform_vector(P, d);

  // get the supporting point from the primitive
  return primitive->get_supporting_point(dir);
}
void FromHoudiniGroupConverter::convertAndAddPrimitive( GU_Detail *geo, GA_PrimitiveGroup *group, GroupPtr &result, const CompoundObject *operands, const std::string &name ) const
{
	GU_Detail childGeo( geo, group );
	for ( GA_GroupTable::iterator<GA_ElementGroup> it=childGeo.primitiveGroups().beginTraverse(); !it.atEnd(); ++it )
	{
		it.group()->clear();
	}
	childGeo.destroyAllEmptyGroups();
	
	PrimitivePtr child = IECore::runTimeCast<Primitive>( doDetailConversion( &childGeo, operands ) );
	if ( child )
	{
		if ( name != "" )
		{
			child->blindData()->member<StringData>( "name", false, true )->writable() = name;
		}
		
		result->addChild( child );
	}
}
asf::auto_release_ptr<asr::Object> IECoreAppleseed::InteractivePrimitiveConverter::doConvertPrimitive( PrimitivePtr primitive, const string &name )
{
	if( primitive->typeId() == MeshPrimitiveTypeId )
	{
		MeshPrimitive *meshPrimitive = static_cast<MeshPrimitive *>( primitive.get() );
		asf::auto_release_ptr<asr::MeshObject> entity( MeshAlgo::convert( meshPrimitive ) );

		if( entity.get() )
		{
			entity->set_name( name.c_str() );
		}
		else
		{
			msg( Msg::Warning, "IECoreAppleseed::PrimitiveConverter", "Couldn't convert object" );
		}

		return asf::auto_release_ptr<asr::Object>( entity.release() );
	}

	return asf::auto_release_ptr<asr::Object>();
}
Пример #11
0
/**
 * This method searches for visualization-filename,
 * visualization-id attributes for
 * a given node and creates a separator based on the attribute found.
 */
osg::Group* Visualizable::construct_from_node(shared_ptr<const XMLTree> node, const std::map<std::string, BasePtr>& id_map)
{  
  std::map<std::string, BasePtr>::const_iterator id_iter; 
  osg::Group* group = NULL;

  // get the relevant attributes
  XMLAttrib* viz_id_attr = node->get_attrib("visualization-id");
  XMLAttrib* vfile_id_attr = node->get_attrib("visualization-filename");

  // check that some visualization data exists
  if (!viz_id_attr && !vfile_id_attr)
    return NULL;

  // check for mix-and-match
  if (viz_id_attr && vfile_id_attr)
  {
    std::cerr << "Visualizable::construct_from_xml() - found mix and match of ";
    std::cerr << "  visualization-id and visualization-filename ";
    std::cerr << "in offending node: " << std::endl << *node;
    return NULL;
  }

  // look for a Primitive or OSGGroup id
  if (viz_id_attr)
  {
    // get the id
    const std::string& id = viz_id_attr->get_string_value();

    // find the ID object
    if ((id_iter = id_map.find(id)) == id_map.end())
    {
      std::cerr << "Visualizable::construct_from_xml() - cannot find ";
      std::cerr << "Primitive/OSGGroup object " << std::endl;
      std::cerr << "  with id '" << id << "' in offending node: " << std::endl;
      std::cerr << *node;
      return NULL;
    }

    // look for it as an OSGGroup 
    OSGGroupWrapperPtr wrapper = boost::dynamic_pointer_cast<OSGGroupWrapper>(id_iter->second);  
    if (wrapper)
    {
      // get the group
      group = wrapper->get_group();
    }
    else
    {
      // it should be a Primitive, if it is not a OSGGroup 
      PrimitivePtr prm = boost::dynamic_pointer_cast<Primitive>(id_iter->second);  
      assert(prm);

      // create the group and add a node to it
      #ifdef USE_OSG
      group = new osg::Group;
      group->addChild(prm->get_visualization());
      #endif
    }
  }
  // visualization-filename attribute
  else
  {
    #ifdef USE_OSG
    // sanity check
    assert(vfile_id_attr);

    // get the filename
    const std::string& fname = vfile_id_attr->get_string_value();

    // create the new OSGGroup wrapper
    OSGGroupWrapperPtr wrapper(new OSGGroupWrapper(fname));

    // get the group-- and reference it 
    group = wrapper->get_group();
    group->ref();
    #endif
  }

  // return the group 
  return group;
}