Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this clipping algorithm code: #include #include #include #include vec.h struct Triangle { vec 4 A , B , C; } ; std::vector Clip

this clipping algorithm code: "#include
#include
#include
#include "vec.h"
struct Triangle
{
vec4 A,B,C;
};
std::vector Clip(const Triangle& tri){
std::vector result;
double planes[6][4]={
{1.0,0.0,0.0,1.0},// Right plane
{-1.0,0.0,0.0,1.0},// Left plane
{0.0,1.0,0.0,1.0},// Top plane
{0.0,-1.0,0.0,1.0},// Bottom plane
{0.0,0.0,1.0,1.0},// Far plane
{0.0,0.0,-1.0,1.0}// Near plane
};
for (int i =0; i <6; ++i){
std::vector vertices;
vertices.push_back(tri.A);
vertices.push_back(tri.B);
vertices.push_back(tri.C);
std::vector clipped;
for (int j =0; j <3; ++j){
vec4 v0= vertices[j];
vec4 v1= vertices[(j +1)%3];
double d0= planes[i][0]* v0[0]+ planes[i][1]* v0[1]+ planes[i][2]* v0[2]+ planes[i][3]* v0[3];
double d1= planes[i][0]* v1[0]+ planes[i][1]* v1[1]+ planes[i][2]* v1[2]+ planes[i][3]* v1[3];
if (d0>=0 && d1>=0){
clipped.push_back(v0);
}
else if (d0>=0 && d1<0){
double t = d0/(d0- d1);
vec4 intersection;
for (int k =0; k <4; ++k){
intersection[k]= v0[k]+(v1[k]- v0[k])* t;
}
clipped.push_back(intersection);
}
else if (d0<0 && d1>=0){
double t = d0/(d0- d1);
vec4 intersection;
for (int k =0; k <4; ++k){
intersection[k]= v0[k]+(v1[k]- v0[k])* t;
}
clipped.push_back(intersection);
}
}
if (clipped.size()>=3){
bool intersectsAnyPlane = false;
for (const auto& vertex : clipped){
double w = vertex[3];
for (int i =0; i <6; ++i){
double d = planes[i][0]* vertex[0]+ planes[i][1]* vertex[1]+ planes[i][2]* vertex[2]+ planes[i][3]* w;
std::cout <<"d: "<< d << std::endl;
if (d <0 && !(i ==5 && d <0)){
intersectsAnyPlane = true;
break;
}
}
if (intersectsAnyPlane){
break;
}
}
if (intersectsAnyPlane){
Triangle clippedTri;
clippedTri.A = clipped[0];
clippedTri.B = clipped[1];
clippedTri.C = clipped[2];
result.push_back(clippedTri);
}
}
}
return result;
}" is giving me these errors: "vertex of clipped triangle is outside clipping volume
input (-0.943233-0.7394280.906402-0.619218)(-0.8932530.453582-0.08159310.218618)(-0.589670.859866-0.723141-0.762994)
output (-0.943233-0.7394280.906402-0.619218)(-0.6306110.805074-0.63662-0.630611)(-0.7128880.302507-0.15524-0.712888)
FAIL" please help me fix it

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions