static void _ringToPoly2tri( const QgsCurve *ring, const QgsPoint &ptFirst, const QMatrix4x4 &toNewBase, std::vector<p2t::Point *> &polyline, QHash<p2t::Point *, float> &zHash ) { QgsVertexId::VertexType vt; QgsPoint pt; const int pCount = ring->numPoints(); double x0 = ptFirst.x(), y0 = ptFirst.y(), z0 = ( std::isnan( ptFirst.z() ) ? 0 : ptFirst.z() ); polyline.reserve( pCount ); for ( int i = 0; i < pCount - 1; ++i ) { ring->pointAt( i, pt, vt ); QVector4D tempPt( pt.x() - x0, pt.y() - y0, std::isnan( pt.z() ) ? 0 : pt.z() - z0, 0 ); QVector4D newBasePt = toNewBase * tempPt; const float x = newBasePt.x(); const float y = newBasePt.y(); const float z = newBasePt.z(); const bool found = std::find_if( polyline.begin(), polyline.end(), [x, y]( p2t::Point *&p ) { return *p == p2t::Point( x, y ); } ) != polyline.end(); if ( found ) { continue; } p2t::Point *pt2 = new p2t::Point( x, y ); polyline.push_back( pt2 ); zHash[pt2] = z; } }
RGBAPixel GeometricObject::rayTrace(PointLight& lightSrc, Point3D& pt, Ray& viewRay, vector<GeometricObject*>& shapes){ RGBAPixel color = material.color; if(texture != NULL) color = *mapToTexture(pt); Vector3D dir(lightSrc.o,pt); dir.normalize(); Point3D tempPt(pt+dir.reflect()*0.01); Ray backtraceRay(tempPt,dir.reflect(), "shadow"); //from hit point on shape to light source double tHitLight = lightSrc.o.distance(pt); bool shadow = false; Point3D trash(0,0,0); for(int i = 0; i < shapes.size(); i++){ if(shapes[i]->isLightSrc) continue; double tHitAnotherShape = shapes[i]->hit(backtraceRay,trash); if(tHitAnotherShape < tHitLight && tHitAnotherShape > 0){ shadow = true; break; } } //pt's intersection with viewRay and geometry Vector3D n(this->getNormal(pt)); n.normalize(); double r = 0.0; double g = 0.0; double b = 0.0; //Ambient Lighting r += 0.04 * 30; g += 0.04 * 30; b += 0.04 * 30; RGBAPixel temp(r*color.red, g*color.green ,b*color.blue); if(shadow){ return temp; } //Diffuse Lighting Vector3D reflectRay = (dir.reflect()).hat(); double product = reflectRay.dot(n); if (product > 0){ r += color.red*product * material.kd; g += color.green*product * material.kd; b += color.blue*product * material.kd; } //Specular Lighting double epsilon = 10.0; Vector3D rVec(dir - (n*dir.dot(n)*2.0)); double spec = rVec.dot(viewRay.d.reflect()); double rw0e; if(spec > 0) rw0e = pow(spec,epsilon); else rw0e = 0; if(product > 0){ r += color.red*rw0e * material.ks * 0.5; g += color.green*rw0e * material.ks * 0.5; b += color.blue*rw0e * material.ks * 0.5; } r = r*material.directScale; g = g*material.directScale; b = b*material.directScale; //Reflections double minTime = 100000.0; double tHitAnotherShape = 0.0; Vector3D recViewDir(viewRay.d - (2*viewRay.d*n)*n); //direction of mirror reflection recViewDir.normalize(); Ray recViewRay(pt,recViewDir, "view"); recViewRay.recurseLevel = viewRay.recurseLevel+1; Point3D recPt; RGBAPixel recColor; //Mirror Reflection if(material.reflectProperty == "mirror" && viewRay.recurseLevel < 3){ GeometricObject* nextShape = NULL; for(int k = 0; k < shapes.size(); k++){ if(shapes[k] == this) continue; tHitAnotherShape = shapes[k]->hit(recViewRay,recPt); if(tHitAnotherShape > 0.0 && tHitAnotherShape < minTime){ nextShape = shapes[k]; minTime = tHitAnotherShape; } } if(nextShape != NULL){ recColor = nextShape->rayTrace(lightSrc, recPt, recViewRay, shapes); r += (recColor.red); //* (1-material.directScale); g += (recColor.green);// * (1-material.directScale); b += (recColor.blue);// * (1-material.directScale); } } if(material.reflectProperty == "glossy" && viewRay.recurseLevel < 3){ double tempR = 0.0; double tempG = 0.0; double tempB = 0.0; Vector3D axisA = Vector3D(1,0,0).cross(recViewDir).hat() * 1; Vector3D axisB = axisA.cross(recViewDir).hat() * 1; Point3D tempPt = pt + recViewDir - 0.5*axisA - 0.5*axisB; Rectangle rect(tempPt, axisA, axisB); vector<Point3D> samplepts = rect.generatePoints(100); for(int i = 0; i < samplepts.size(); i++){ Vector3D indirectDir(pt,samplepts[i]); Ray indirectRay(pt,indirectDir); indirectRay.recurseLevel = viewRay.recurseLevel + 1; GeometricObject* nextShape = NULL; double minTime = 100000.0; double tHitAnotherShape = 0.0; for(int k = 0; k < shapes.size(); k++){ if(shapes[k] == this) continue; tHitAnotherShape = shapes[k]->hit(indirectRay,recPt); if(tHitAnotherShape > 0.0 && tHitAnotherShape < minTime){ nextShape = shapes[k]; minTime = tHitAnotherShape; } if(nextShape != NULL && nextShape->material.transparency == 0){ recColor = nextShape->rayTrace(lightSrc, recPt, recViewRay, shapes); tempR += (recColor.red); tempG += (recColor.green); tempB += (recColor.blue); } } } r += tempR / samplepts.size(); g += tempG / samplepts.size(); b += tempB / samplepts.size(); } if(material.transparency > 0 && viewRay.recurseLevel == 0){ double tempR = 255; double tempG = 255; double tempB = 255; Vector3D invNormal = n.reflect(); Ray inverseRay(pt,invNormal); inverseRay.recurseLevel = viewRay.recurseLevel + 1; GeometricObject* nextShape = NULL; double minTime = 100000.0; double tHitAnotherShape = 0.0; for(int k = 0; k < shapes.size(); k++){ if(shapes[k] == this) continue; tHitAnotherShape = shapes[k]->hit(inverseRay,recPt); if(tHitAnotherShape > 0.0 && tHitAnotherShape < minTime){ nextShape = shapes[k]; minTime = tHitAnotherShape; } if(nextShape != NULL && nextShape != this){ recColor = nextShape->rayTrace(lightSrc, recPt, inverseRay, shapes); tempR = (recColor.red); tempG = (recColor.green); tempB = (recColor.blue); } } r = tempR * material.transparency + (r*(1-material.transparency)); g = tempG * material.transparency + (g*(1-material.transparency)); b = tempB * material.transparency + (b*(1-material.transparency)); } //cap off maximum color values r =std::min((int)r,255); g =std::min((int)g,255); b =std::min((int)b,255); temp(r,g,b); return temp; }
RGBAPixel Sphere::castDirectionalLight(DirectionalLight& lightSrc, Point3D& pt, Ray& viewRay, vector<GeometricObject*>& shapes){ Point3D tempPt(pt+lightSrc.dir.reflect()*0.01); Ray backtraceRay(tempPt,lightSrc.dir.reflect(),"shadow"); double tHitLight = std::numeric_limits<double>::max(); bool shadow = false; Point3D trash(0,0,0); for(int i = 0; i < shapes.size(); i++){ double tHitAnotherShape = shapes[i]->hit(backtraceRay,trash); if(tHitAnotherShape < tHitLight && tHitAnotherShape > 0){ shadow = true; break; } } //pt's intersection with viewRay and sphere Vector3D normal(c, pt); //normal from center to hitPoint normal.normalize(); double r = 0.0; double g = 0.0; double b = 0.0; //Ambient Lighting r += 0.08 * 30; g += 0.08 * 30; b += 0.08 * 30; RGBAPixel temp(r*material.color.red, r*material.color.green ,r*material.color.blue); if(shadow){ return temp; } //Diffuse Lighting Vector3D reflectRay = (lightSrc.dir.reflect()).hat(); double product = reflectRay.dot(normal); if (product <= 0){ temp(r,g,b); return temp; } r += material.color.red*product * material.kd; g += material.color.green*product * material.kd; b += material.color.blue*product * material.kd; //Specular Lighting double epsilon = 10.0; Vector3D rVec(lightSrc.dir - normal*lightSrc.dir.dot(normal)*2.0); double rw0e = pow(rVec.dot(viewRay.d.reflect()),epsilon); r += material.color.red*rw0e * material.ks; g += material.color.green*rw0e * material.ks; b += material.color.blue*rw0e * material.ks; //cap off maximum color values r =std::min((int)r,255); g =std::min((int)g,255); b =std::min((int)b,255); temp(r,g,b); return temp; }
void defineBlockWithAttributes( AcDbObjectId& blockId, // This is a returned value. const AcGePoint3d& basePoint, double textHeight, double textAngle) { int retCode = 0; AcDbBlockTable *pBlockTable = NULL; AcDbBlockTableRecord* pBlockRecord = new AcDbBlockTableRecord; AcDbObjectId entityId; // Step 1: Set the block name and base point of the block definition // pBlockRecord->setName("ASDK-BLOCK-WITH-ATTR"); pBlockRecord->setOrigin(basePoint); // Open the block table for write. // acdbHostApplicationServices()->workingDatabase() ->getSymbolTable(pBlockTable, AcDb::kForWrite); // Step 2: Add the block table record to block table. // pBlockTable->add(blockId, pBlockRecord); // Step 3: Create a circle entity. // AcDbCircle *pCircle = new AcDbCircle; pCircle->setCenter(basePoint); pCircle->setRadius(textHeight * 4.0); pCircle->setColorIndex(3); // Append the circle entity to the block record. // pBlockRecord->appendAcDbEntity(entityId, pCircle); pCircle->close(); // Step 4: Create an attribute definition entity. // AcDbAttributeDefinition *pAttdef = new AcDbAttributeDefinition; // Set the attribute definition values. // pAttdef->setPosition(basePoint); pAttdef->setHeight(textHeight); pAttdef->setRotation(textAngle); pAttdef->setHorizontalMode(AcDb::kTextLeft); pAttdef->setVerticalMode(AcDb::kTextBase); pAttdef->setPrompt("Prompt"); pAttdef->setTextString("DEFAULT"); pAttdef->setTag("Tag"); pAttdef->setInvisible(Adesk::kFalse); pAttdef->setVerifiable(Adesk::kFalse); pAttdef->setPreset(Adesk::kFalse); pAttdef->setConstant(Adesk::kFalse); pAttdef->setFieldLength(25); // Append the attribute definition to the block. // pBlockRecord->appendAcDbEntity(entityId, pAttdef); // The second attribute definition is a little easier // because we are cloning the first one. // AcDbAttributeDefinition *pAttdef2 = AcDbAttributeDefinition::cast(pAttdef->clone()); // Set the values which are specific to the // second attribute definition. // AcGePoint3d tempPt(basePoint); tempPt.y -= pAttdef2->height(); pAttdef2->setPosition(tempPt); pAttdef2->setColorIndex(1); // Red pAttdef2->setConstant(Adesk::kTrue); // Append the second attribute definition to the block. // pBlockRecord->appendAcDbEntity(entityId, pAttdef2); pAttdef->close(); pAttdef2->close(); pBlockRecord->close(); pBlockTable->close(); return; }