本文整理汇总了C++中Hit::set方法的典型用法代码示例。如果您正苦于以下问题:C++ Hit::set方法的具体用法?C++ Hit::set怎么用?C++ Hit::set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Hit
的用法示例。
在下文中一共展示了Hit::set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: intersect
bool Sphere::intersect(const Ray &r, Hit &h, float tmin)
{
Vec3f v = center - r.getOrigin();
float tp = v.Dot3(r.getDirection());
float det = tp*tp - v.Dot3(v) + radius*radius;
//intersect
if(det > 0)
{
//t'
det = sqrtf(det);
float t1 = tp - det;
float t2 = tp + det;
if(t1 > tmin && t1 < h.getT())
{
Vec3f normal = (r.pointAtParameter(t1) - center);
normal /= radius;
normal.Normalize();
h.set(t1,material,normal,r);
return 1;
}
else if(t2 > tmin && t2 < h.getT())
{
//sphere's normal
Vec3f normal = (r.pointAtParameter(t2) - center);
normal /= radius;
normal.Normalize();
h.set(t2,material,normal,r);
return 1;
}
}
return 0;
}
示例2: intersect
/*
射线与平面求教
即直线的参数方程P = ori + t*dir
代入 平面方程 n * p + D =0
n(R0+t*Rd)+D=0
t=-(D+n*R0)/n*Rd
hit 带走的信息包括:
float t;
Material *material;
Vec3f normal;
Vec3f intersectionPoint;
*/
bool Plane::intersect(const Ray &r, Hit &h, float tmin)
{
/*
需要判断射线是否与平面平行
如果t<0说明平面在视点之后
以上两个都不产生交点
*/
float eps = 1e-6;
float tmp = normal.Dot3(r.getDirection());
if (fabs(tmp) < eps)
return false;
float t = 0.0f;
Vec3f r0 = r.getOrigin();
Vec3f rd = r.getDirection();
//这里一直不明白
t = (d - normal.Dot3(r0)) / normal.Dot3(rd);
if (t < 0)
return false;
h.set(t, material, normal, r);
if (t >= tmin)
return true;
else
return false;
}
示例3: intersect
bool Triangle::intersect(const Ray &r, Hit &h, float tmin)
{
Vec3f r0 = r.getOrigin();
Vec3f rd = r.getDirection();
Vec3f E1 = a - b;
Vec3f E2 = a - c;
Vec3f S = a - r0;
//参数写错,rd写成r0了……
float de = det3x3(rd.x(), rd.y(), rd.z(), E1.x(), E1.y(), E1.z(), E2.x(), E2.y(), E2.z());
if (de == 0.0f)
return false;
float t = det3x3(S.x(), S.y(), S.z(), E1.x(), E1.y(), E1.z(), E2.x(), E2.y(), E2.z())/de;
float belta = det3x3(rd.x(), rd.y(), rd.z(), S.x(), S.y(), S.z(), E2.x(), E2.y(), E2.z()) / de;
float lamda = det3x3(rd.x(), rd.y(), rd.z(), E1.x(), E1.y(), E1.z(), S.x(), S.y(), S.z()) / de;
Vec3f normal;
Vec3f::Cross3(normal, b - a, c - a);
normal.Normalize();
h.set(t, material, normal, r);
if (t >= tmin && belta > 0.0f && lamda > 0.0f && belta + lamda < 1.0f)
return true;
else
return false;
}
示例4: intersect
bool Grid::intersect(const Ray &r, Hit &h, float tmin)
{
//Prepare the Material objects
Vec3f diffuseColor(1, 1, 1);
Vec3f specularColor(0, 0, 0);
float exponent = 1;
Vec3f reflectiveColor(0, 0, 0);
Vec3f transparentColor(0, 0, 0);
float indexOfRefraction = 1;
PhongMaterial *m = new PhongMaterial(diffuseColor, specularColor, exponent,
reflectiveColor, transparentColor, indexOfRefraction);
//Start to do DDA
MarchingInfo mi;
initializeRayMarch(mi, r, tmin);
bool hitSomething = false;
int i, j, k;
do {
mi.getIndices(i, j, k);
printf("The current point is:%d,%d,%d\n", i, j, k);
if (mObjects[offset(i, j, k)].getNumObjects() > 0) {
h.set(mi.get_tmin(), m, mi.getNormal(), r);
return true;
}
mi.nextCell();
} while(i < mXSize && j < mYSize && k < mZSize);
return false;
}
示例5: intersect
bool Plane::intersect(const Ray & r, Hit & h, float tmin)
{
assert (tmin >= 0.0f);
Vec3f org = r.getOrigin();
Vec3f dir = r.getDirection();
if (dir.Dot3(normal) < 1.0e-7 && dir.Dot3(normal) > -1.0e-7) {
return false;
} // Appromately parrell to plane
float tempT = (offset - org.Dot3(normal)) / dir.Dot3(normal);
if (tempT <= 1e-6) {
return false;
}
else if (tempT >= tmin && tempT < h.getT())
{
// Update Hit Point
normal.Normalize();
h.set(tempT, NULL, normal, color, r);
return true;
}
return false;
}
示例6: plane_intersect
bool Face::plane_intersect(const Ray &r, Hit &h, bool intersect_backfacing) const {
// insert the explicit equation for the ray into the implicit equation of the plane
// equation for a plane
// ax + by + cz = d;
// normal . p + direction = 0
// plug in ray
// origin + direction * t = p(t)
// origin . normal + t * direction . normal = d;
// t = d - origin.normal / direction.normal;
glm::vec3 normal = computeNormal();
float d = glm::dot(normal,(*this)[0]->get());
float numer = d - glm::dot(r.getOrigin(),normal);
float denom = glm::dot(r.getDirection(),normal);
if (denom == 0) return 0; // parallel to plane
if (!intersect_backfacing && glm::dot(normal,r.getDirection()) >= 0)
return 0; // hit the backside
float t = numer / denom;
if (t > EPSILON && t < h.getT()) {
h.set(t,this->getMaterial(),normal);
assert (h.getT() >= EPSILON);
return 1;
}
return 0;
}
示例7: Intersect
bool Transform::Intersect(const Ray &r, Hit &h, float tmin) const
{
bool result = false;
Matrix m = m_matrix;
if ( m.Inverse() )
{
Vec3f org = r.getOrigin();
Vec3f dir = r.getDirection();
m.Transform(org);
m.TransformDirection(dir);
Ray r2 (dir, org);
result = m_pObject->Intersect(r2, h, tmin);
if (result)
{
Matrix m1 = m;
m1.Transpose();
Vec3f n = h.getNormal();
m1.TransformDirection(n);
n.Normalize();
h.set(h.getT(), h.getMaterial(), n, r);
}
}
return result;
}
示例8: plane_intersect
bool Face::plane_intersect(const Ray &r, Hit &h, bool intersect_backfacing, bool* backfacing_hit) {
// insert the explicit equation for the ray into the implicit equation of the plane
// equation for a plane
// ax + by + cz = d;
// normal . p + direction = 0
// plug in ray
// origin + direction * t = p(t)
// origin . normal + t * direction . normal = d;
// t = d - origin.normal / direction.normal;
Vec3f normal = computeNormal();
double d = normal.Dot3((*this)[0]->get());
double numer = d - r.getOrigin().Dot3(normal);
double denom = r.getDirection().Dot3(normal);
if (denom == 0) return 0; // parallel to plane
if (!intersect_backfacing && normal.Dot3(r.getDirection()) >= 0)
return 0; // hit the backside
double t = numer / denom;
if (t > EPSILON && t < h.getT()) {
h.set(t,this->getMaterial(),normal,this);
assert (h.getT() >= EPSILON);
//hit the backside but that's okay in this case
if (normal.Dot3(r.getDirection()) >= 0){
*backfacing_hit = true;
}
return 1;
}
return 0;
}
示例9: intersect
bool Sphere::intersect(const Ray &r, Hit &h, float tmin)
{
//直线方程 P = t*D + R ①
//圆方程 ||P||= raduis ②
//将①带入②, 由点乘的性质(满足分配率,交换律),求得t^2+2RDt+R^2-r^2=0
//得t = -RD±sqrt(RD^2-R^2+r^2)
//选择距离较近的那个点t = -RD-sqrt(RD^2-R^2+r^2)
Vec3f D = r.getDirection();
Vec3f R = r.getOrigin()-center;
float R2 = R.Dot3(R);
float RD = R.Dot3(D);
float b2_4ac = RD*RD-R2+radius*radius;//R2 RD RDRD这些其实可以存在ray里,但是算法研究以程序清晰为要
if(b2_4ac<0)return 0;
float t;
t = -RD - sqrt(b2_4ac);
if(t<0){
t = -RD + sqrt(b2_4ac);
if(t < 0)return 0;
}
if(t<h.getT())
{
h.set(t, &color, r);
}
return 1;
}
示例10: intersect
/*
The intersect routine will first transform the ray,
then delegate to the intersect routine of the contained
object. Make sure to correctly transform the resulting
normal according to the rule seen in lecture. You may
choose to normalize the direction of the transformed ray
or leave it un-normalized. If you decide not to normalize
the direction, you might need to update some of your intersection code.
*/
bool Transform::intersect(const Ray &r, Hit &h, float tmin)
{
Vec3f r0 = r.getOrigin();
Vec3f rd = r.getDirection();
Matrix inv;
matrix.Inverse(inv);
inv.Transform(r0);
inv.TransformDirection(rd);
if (object != NULL)
{
//这里的h是有问题的,作如下修改:
bool judge = object->intersect(Ray(r0,rd), h, tmin);
Vec3f normal = h.getNormal();
//这里很奇怪,normal的方向没有修正,然而结果却是对的
//改了之后反而是错的!!
//这里确定normal没有错,那么就是之后应用normal的
//问题
//好吧,就是这里的问题
//经过把图形摆正,发现求的法向量没有问题,但是没有单位化…………!
matrix.TransformDirection(normal);
normal.Normalize();
//or:
//Matrix change,res;
//matrix.Inverse(change);
//change.Transpose(res);
//res.TransformDirection(normal);
h.set(h.getT(), h.getMaterial(), normal, r);
return judge;
}
return false;
}
示例11: intersect
bool Sphere::intersect(const Ray &r, Hit &h, float tmin){
/*Vector equation for sphere
P*P - r^2 = 0
((R0-c) + td)*((R0-c) + td) - r^2 = 0
(td)^2 + 2R0-c)td + (R0-c)^2 - r^2 =0
Use quadratic equation solve for t
a = (d^2)
b = 2(R0-c)d
c = ((R0-c)^2 - r^2)
*/
Vec3f rayOrigin, rayDirection,segment,Q;
float tpos,tneg,a,b,c,discriminant;
rayOrigin = r.getOrigin();
rayDirection = r.getDirection();
segment = rayOrigin - _center;
a = rayDirection.Dot3(rayDirection);
b = 2*segment.Dot3(rayDirection);
c = segment.Dot3(segment) - _radius*_radius;
discriminant = sqrt(b*b - 4*a*c);
tpos = (-b + discriminant)/(2*a);
tneg = (-b - discriminant)/(2*a);
if (tneg > tmin && tneg < h.getT()){
Q = r.pointAtParameter(tneg);
Q = Q - _center;
Q.Normalize();
h.set(tneg, _material,Q,r);
return true;
}
else if (tpos > tmin && tpos < h.getT()){
Q = r.pointAtParameter(tpos);
Q = Q - _center;
Q.Normalize();
h.set(tpos,_material,Q,r);
return true;
}
return false;
}
示例12: if
void Track::extrapolateToLayer0() {
Int_t nTracks = GetEntriesFast();
Int_t eventID = -1;
if (!At(0)) {
// OK, no information in first layer
// now GetLayer(1) should be 1, and GetLayer(2) should be 2
// If both layer 0 and 2 are skipped, some more extrapolation should be done
// maybe set new x as [1.x - slope.x * (2.z - 1.z)]
Hit *slope = new Hit();
if (!At(1)) {
cout << "No pointer for At(1) as well... Aborting this track extrapolation.\n";
return;
}
else {
eventID = At(1)->getEventID();
if (getLayer(1) == 0) return; // hotfix... Why is layer 0 sometimes placed in idx 1? Must fix this.
}
if (At(2)) {
slope->set(getX(2) - getX(1), getY(2) - getY(1), getLayer(2) - getLayer(1));
if (eventID<0) eventID = At(2)->getEventID();
}
else if (!At(2) && At(3)) {
slope->set(getX(3) - getX(1), getY(3) - getY(1), getLayer(3) - getLayer(1));
if (eventID<0) eventID = At(3)->getEventID();
}
else {
cout << "Too many holes (!At(0), At(1), !At(2), !At(3), .....), aborting this track extrapolation.\n";
return;
}
// must create pointer!
Cluster *newStart = (Cluster*) track_.ConstructedAt(0);
newStart->set(getX(1) - slope->getLayer() * slope->getX(), // new x
getY(1) - slope->getLayer() * slope->getY(), // new y
0); // layer = 0
newStart->setEventID(eventID);
}
}
示例13: intersect
bool Sphere::intersect(const Ray &r, Hit &h) const {
// ==========================================
// ASSIGNMENT: IMPLEMENT SPHERE INTERSECTION
// ==========================================
// a = d (dot) d
double a = r.getDirection().Dot3(r.getDirection());
// b = 2d (dot) (orginPoint - centerPoint)
double b = (2*(r.getDirection())).Dot3(r.getOrigin() - center);
// c = (p_0 - p_c) dot (p_0-p_c) - r^2
double c = (r.getOrigin() - center).Dot3(r.getOrigin() - center) - (radius*radius);
// t = (-b +/- sqrt(b2 - 4 a c)) / (2 a)
// if inside is negative, then it doesn't intersect the sphere
// if zero just a slight glance of sphere
// if two then you interect and leave
double inside = (b*b) - 4*a*c;
if(inside >= 0 ){
//inside
// get the first intersection point
double t = ((-1*b) - sqrt(inside)) / (2*a);
if(t < 0) return false;
// get pt collision
Vec3f pt = r.getOrigin() + t * r.getDirection();
if(pt.Distance3f(r.getOrigin()) < EPSILON) return false;
Vec3f norm((pt.x() - center.x())/radius, (pt.y() - center.y())/radius, (pt.z() - center.z())/radius);
norm.Normalize();
h.set(t,getMaterial(),norm);
return true;
}else{
// Negative and therefore missed
return false;
}
// return true if the sphere was intersected, and update
// the hit data structure to contain the value of t for the ray at
// the intersection point, the material, and the normal
return false;
}
示例14: intersect
bool Triangle::intersect( const Ray& r , Hit& h , float tmin){
Vector3f R_o = r.getOrigin();
Vector3f R_d = r.getDirection();
Matrix3f A( this->a.x()-this->b.x() , this->a.x()-this->c.x() , R_d.x() ,
this->a.y()-this->b.y() , this->a.y()-this->c.y() , R_d.y() ,
this->a.z()-this->b.z() , this->a.z()-this->c.z() , R_d.z() );
Matrix3f BetaM( this->a.x()-R_o.x() , this->a.x()-this->c.x() , R_d.x() ,
this->a.y()-R_o.y() , this->a.y()-this->c.y() , R_d.y() ,
this->a.z()-R_o.z() , this->a.z()-this->c.z() , R_d.z() );
float beta = BetaM.determinant() / A.determinant();
Matrix3f GammaM( this->a.x()-this->b.x() , this->a.x()-R_o.x() , R_d.x() ,
this->a.y()-this->b.y() , this->a.y()-R_o.y() , R_d.y() ,
this->a.z()-this->b.z() , this->a.z()-R_o.z() , R_d.z() );
float gamma = GammaM.determinant() / A.determinant();
float alpha = 1.0f - beta - gamma;
Matrix3f tM( this->a.x()-this->b.x() , this->a.x()-this->c.x() , this->a.x()-R_o.x() ,
this->a.y()-this->b.y() , this->a.y()-this->c.y() , this->a.y()-R_o.y() ,
this->a.z()-this->b.z() , this->a.z()-this->c.z() , this->a.z()-R_o.z() );
float t = tM.determinant() / A.determinant();
if (beta + gamma > 1){
return false;
}
if (beta < 0){
return false;
}
if (gamma < 0){
return false;
}
if (t > tmin && t < h.getT()){
Vector3f newNormal = (alpha*this->normals[0] + beta*this->normals[1] + gamma*this->normals[2]).normalized();
h.set(t, this->material, newNormal);
Vector2f newTexCoord = (alpha*this->texCoords[0] + beta*this->texCoords[1] + gamma*this->texCoords[2]);
h.setTexCoord(newTexCoord);
return true;
}
else{
return false;
}
}
示例15: intersect
bool CylinderRing::intersect(const Ray &r, Hit &h) const {
// intersect with the 4 parts of the ring
double outer_t;
Vec3f outer_normal;
bool outer = IntersectFiniteCylinder(r,center,outer_radius,height,outer_t,outer_normal);
double inner_t;
Vec3f inner_normal;
bool inner = IntersectFiniteCylinder(r,center,inner_radius,height,inner_t,inner_normal);
double top_t;
Vec3f top_normal;
bool top = IntersectAnnulus(r,center+Vec3f(0,height/2.0,0),inner_radius,outer_radius,top_t,top_normal);
double bottom_t;
Vec3f bottom_normal;
bool bottom = IntersectAnnulus(r,center-Vec3f(0,height/2.0,0),inner_radius,outer_radius,bottom_t,bottom_normal);
bool answer = false;
// return the closest intersection
if (outer && (outer_t < h.getT())) {
h.set(outer_t,this->getMaterial(),outer_normal,NULL);
answer = true;
}
if (inner && (inner_t < h.getT())) {
h.set(inner_t,this->getMaterial(),-inner_normal,NULL);
answer = true;
}
if (top && (top_t < h.getT())) {
h.set(top_t,this->getMaterial(),top_normal,NULL);
answer = true;
}
if (bottom && (bottom_t < h.getT())) {
h.set(bottom_t,this->getMaterial(),-bottom_normal,NULL);
answer = true;
}
return answer;
}