/// Return false if the particle is dead bool advance(float dt) { if (curtime >= t0 + maxlife) return false; // gravity vit.z() -= gravity * dt; // update pos += vit * dt; // collision if (pos.z() < 0) { float dv = vit.z(); pos.z() = 0; if (dv < 0) { vit.z() = -0.8f * vit.z(); last_col = curtime; //-d; } } // color float c = curtime - last_col; if (c > 1) c = 1; color.x() = 1 - c; return true; }
bool AABBox<Point3f>::intersection<Point3f>(Point3f const & point, float tolerance) { if (point.x() + tolerance < m_minCor.x() || point.x() - tolerance > m_maxCor.x()) return false; if (point.y() + tolerance < m_minCor.y() || point.y() - tolerance > m_maxCor.y()) return false; if (point.z() + tolerance < m_minCor.z() || point.z() - tolerance > m_maxCor.z()) return false; return true; }
float AABBox<Point3f>::squared_distance<Point3f>(Point3f const & point) { if (this->intersection<Point3f>(point, 0.0f)) return 0.0f; float delta_x = (std::max)((std::max)(this->m_minCor.x() - point.x(), point.x() - this->m_maxCor.x()), 0.0f); float delta_y = (std::max)((std::max)(this->m_minCor.y() - point.y(), point.y() - this->m_maxCor.y()), 0.0f); float delta_z = (std::max)((std::max)(this->m_minCor.z() - point.z(), point.z() - this->m_maxCor.z()), 0.0f); //std::cout << "delta x " << delta_x << " delta y " << delta_y << " delta z " << delta_z << std::endl; return delta_x * delta_x + delta_y * delta_y + delta_z * delta_z; }
WavefrontOBJ(const PropertyList &propList) { typedef boost::unordered_map<OBJVertex, uint32_t, OBJVertexHash> VertexMap; /* Process the OBJ-file line by line */ QString filename = propList.getString("filename"); QFile input(filename); if (!input.open(QIODevice::ReadOnly | QIODevice::Text)) throw NoriException(QString("Cannot open \"%1\"").arg(filename)); Transform trafo = propList.getTransform("toWorld", Transform()); cout << "Loading \"" << qPrintable(filename) << "\" .." << endl; m_name = filename; QTextStream stream(&input); QTextStream line; QString temp, prefix; std::vector<Point3f> positions; std::vector<Point2f> texcoords; std::vector<Normal3f> normals; std::vector<uint32_t> indices; std::vector<OBJVertex> vertices; VertexMap vertexMap; while (!(temp = stream.readLine()).isNull()) { line.setString(&temp); line >> prefix; if (prefix == "v") { Point3f p; line >> p.x() >> p.y() >> p.z(); p = trafo * p; positions.push_back(p); } else if (prefix == "vt") {
/** * \brief Evaluate sigma_t(p), where 'p' is given in local coordinates * * You may assume that the maximum value returned by this function is * equal to 'm_densityMultiplier' */ float lookupSigmaT(const Point3f &_p) const { Point3f p = _p.cwiseProduct(m_resolution.cast<float>()), pf = Point3f(std::floor(p.x()), std::floor(p.y()), std::floor(p.z())); Point3i p0 = pf.cast<int>(); if ((p0.array() < 0).any() || (p0.array() >= m_resolution.array() - 1).any()) return 0.0f; size_t row = m_resolution.x(), slab = row * m_resolution.y(), offset = p0.z()*slab + p0.y()*row + p0.x(); const float d000 = m_data[offset], d001 = m_data[offset + 1], d010 = m_data[offset + row], d011 = m_data[offset + row + 1], d100 = m_data[offset + slab], d101 = m_data[offset + slab + 1], d110 = m_data[offset + slab + row], d111 = m_data[offset + slab + row + 1]; Vector3f w1 = p-pf, w0 = (1 - w1.array()).matrix(); /* Trilinearly interpolate */ return (((d000 * w0.x() + d001 * w1.x()) * w0.y() + (d010 * w0.x() + d011 * w1.x()) * w1.y()) * w0.z() + ((d100 * w0.x() + d101 * w1.x()) * w0.y() + (d110 * w0.x() + d111 * w1.x()) * w1.y()) * w1.z()) * m_densityMultiplier; }
void GraphicsPainter::drawCircle(const Point3f ¢er, float radius, const Vector3f &normal) { Vector3f right(normal.y(), -normal.x(), 0); // vector orthogonal to normal glBegin(GL_TRIANGLE_FAN); glNormal3f(normal.x(), normal.y(), normal.z()); glVertex3f(center.x(), center.y(), center.z()); for(float angle = 0; angle <= 360; angle += 10){ Point3f point = center + (chemkit::geometry::rotate(right, normal, angle).normalized() * radius); glNormal3f(normal.x(), normal.y(), normal.z()); glVertex3f(point.x(), point.y(), point.z()); } glEnd(); }
void GraphicsPainter::drawSphere(const Point3f ¢er, float radius) { glPushMatrix(); glTranslated(center.x(), center.y(), center.z()); drawSphere(radius); glPopMatrix(); }
void init(float _t0) { pos.x() = frand(0.1); pos.y() = frand(0.1); pos.z() = 0.00; int angle = nrand(minangle, maxangle); float angle_rad = (float(angle)/180.0f)*3.14159265f; vit.x() = ::cos(angle_rad); vit.y() = ::sin(angle_rad); vit.z() = 0; // vit.x() = frand(2)-1; vit.y() = frand(2)-1; vit.z() = 0; vit.normalize(); vit.z() = 2+frand(2); color.x() = 0; color.y() = 0.5f+frand(0.5f); color.z() = 0.9f+frand(0.1f); t0 = _t0; last_col = t0; }
/// Multiplies \p point by the inverse of the transform. Point3f GraphicsTransform::inverseMultiplyPoint(const Point3f &point) const { Eigen::Matrix<float, 4, 1> vector4; vector4[0] = point.x(); vector4[1] = point.y(); vector4[2] = point.z(); vector4[3] = 1; vector4 = m_matrix->inverse() * vector4; return Point3f(vector4[0], vector4[1], vector4[2]); }
/// Returns the depth of point in the scene. float GraphicsView::depth(const Point3f &point) const { Eigen::Matrix<float, 4, 1> viewPoint; viewPoint[0] = point.x(); viewPoint[1] = point.y(); viewPoint[2] = point.z(); viewPoint[3] = 1; GraphicsTransform transform = projectionTransform() * modelViewTransform(); viewPoint = transform.multiply(viewPoint); viewPoint *= 1.0 / viewPoint[3]; float winZ = (viewPoint[2] + 1) / 2; return winZ; }
void GraphicsPainter::drawCylinder(const Point3f &a, const Point3f &b, float radius) { glPushMatrix(); glTranslatef(a.x(), a.y(), a.z()); Vector3f vector = (a - b).normalized(); Vector3f axis = vector.cross(-Vector3f::UnitZ()).normalized(); float angle = chemkit::geometry::angle(vector.cast<Real>(), -Vector3f::UnitZ().cast<Real>()); glRotatef(-angle, axis.x(), axis.y(), axis.z()); float length = chemkit::geometry::distance(a.cast<Real>(), b.cast<Real>()); drawCylinder(radius, length); glPopMatrix(); }
void Cube::update(const Vector3f &min, const Vector3f &max) { std::string v = std::to_string(max.x()) + " " + std::to_string(min.y()) + " " + std::to_string(min.z()) + "\n" + std::to_string(max.x()) + " " + std::to_string(min.y()) + " " + std::to_string(max.z()) + "\n" + std::to_string(min.x()) + " " + std::to_string(min.y()) + " " + std::to_string(max.z()) + "\n" + std::to_string(min.x()) + " " + std::to_string(min.y()) + " " + std::to_string(min.z()) + "\n" + std::to_string(max.x()) + " " + std::to_string(max.y()) + " " + std::to_string(min.z()) + "\n" + std::to_string(max.x()) + " " + std::to_string(max.y()) + " " + std::to_string(max.z()) + "\n" + std::to_string(min.x()) + " " + std::to_string(max.y()) + " " + std::to_string(max.z()) + "\n" + std::to_string(min.x()) + " " + std::to_string(max.y()) + " " + std::to_string(min.z()) + "\n"; std::istringstream is(v); std::string line_str; std::vector<Vector3f> newPos; m_bbox.reset(); while (std::getline(is, line_str)) { std::istringstream line(line_str); Point3f p; line >> p.x() >> p.y() >> p.z(); newPos.push_back(p); m_bbox.expandBy(p); } for (uint32_t i = 0; i < m_V.cols(); ++i) m_V.col(i) = newPos.at(vertices[i].p - 1); // Update data on GPU glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER]); // Update data only void* ptr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); if (ptr) { memcpy(ptr, (const uint8_t *)m_V.data(), 3 * m_V.cols() * sizeof(GLfloat)); glUnmapBuffer(GL_ARRAY_BUFFER); } glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); }
/// Projects a point from the scene to the window. QPointF GraphicsView::project(const Point3f &point) const { Eigen::Matrix<float, 4, 1> vector; vector[0] = point.x(); vector[1] = point.y(); vector[2] = point.z(); vector[3] = 0; GraphicsTransform transform = projectionTransform() * modelViewTransform(); vector = transform.multiply(vector); vector *= 1.0 / vector[3]; float winX = width() * (vector[0] + 1) / 2; float winY = height() * (vector[1] + 1) / 2; float winZ = (vector[2] + 1) / 2; // if winZ is greater than 1.0 the point is not // visible (it is either in front of the near clip // plane or behind the far clip plane). if(winZ > 1.0) return QPointF(); else return QPointF(winX, height() - winY); }
Point3f::Point3f(const Point3f &_p) : x_(_p.x()), y_(_p.y()), z_(_p.z()) {}
Point3f operator-(const Point3f &_p, const Vector3f &_v) { return Point3f(_p.x()-_v.x(), _p.y()-_v.y(), _p.z()-_v.z()); }
Vector3f operator-(const Point3f &_p1, const Point3f &_p2) { return Vector3f(_p1.x()-_p2.x(), _p1.y()-_p2.y(), _p1.z()-_p2.z()); }