Ejemplo n.º 1
0
 def __init__(self, *args):
     if len(args) == 2:  # 点法式构造  A(x-x0)+B(y-y0)+C(z-z0)=0
         xPoint, xVector = args
         self.__point0 = deepcopy(xPoint)
         if xVector.z == 0:
             self.__point1 = deepcopy(self.__point0)
             self.__point1.z += 1
             self.__point2 = Point3D(0, 0, 0)
             self.__point2.x = self.__point0.x - (self.__point1.z - self.__point0.z) * xVector.y
             self.__point2.y = self.__point0.y + (self.__point1.z - self.__point0.z) * xVector.x
             self.__point2.z = self.__point0.z + (self.__point1.x - self.__point0.x) * xVector.y \
                                                 - (self.__point1.y - self.__point0.y) * xVector.x
         else:
             temp = Operations.dotMultiply(xPoint, xVector)
             self.__point1 = Point3D(xPoint.x, xPoint.y + 1, 0)
             self.__point2 = Point3D(xPoint.x + 1, xPoint.y, 0)
             self.__point1.z = (temp - xVector.x * self.__point1.x - xVector.y * self.__point1.y) / xVector.z
             self.__point2.z = (temp - xVector.x * self.__point2.x - xVector.y * self.__point2.y) / xVector.z
     elif len(args) == 3:  # 三点式构造
         self.__point0 = args[0]
         self.__point1 = args[1]
         self.__point2 = args[2]
     else:
         self.__point0 = None
         self.__point1 = None
         self.__point2 = None
Ejemplo n.º 2
0
 def __init__(self,
              xVertex1=Point3D(),
              xVertex2=Point3D(),
              xVertex3=Point3D()):
     assert isinstance(xVertex1, Point3D) and isinstance(
         xVertex2, Point3D) and isinstance(xVertex3, Point3D)
     self.__vertex1 = deepcopy(xVertex1)
     self.__vertex2 = deepcopy(xVertex2)
     self.__vertex3 = deepcopy(xVertex3)
Ejemplo n.º 3
0
 def __init__(self,
              xMaxPoint=Point3D(1, 1, 1),
              xMinPoint=Point3D(-1, -1, -1)):
     """
     Box初始化,两个点构造一个与世界坐标系平行的Box
     :param xMaxPoint:
     :param xMinPoint:
     """
     assert isinstance(xMaxPoint, Point3D) and isinstance(
         xMinPoint, Point3D)
     self.__maxPoint = deepcopy(xMaxPoint)
     self.__minPoint = deepcopy(xMinPoint)
Ejemplo n.º 4
0
 def conjugateQuaternion(self):
     # 共轭四元数
     tempS = self.__s
     tempVx = -self.__v.x
     tempVy = -self.__v.y
     tempVz = -self.__v.z
     return Quaternion(tempS, Point3D(tempVx, tempVy, tempVz))
Ejemplo n.º 5
0
 def __mul__(self, other):
     assert isinstance(other, Quaternion)
     tempS = self.__s * other.__s - self.__v.x * other.__v.x - self.__v.y * other.__v.y - self.__v.z * other.__v.z
     tempVx = self.__s * other.__v.x + self.__v.x * other.__s + self.__v.y * other.__v.z - self.__v.z * other.__v.y
     tempVy = self.__s * other.__v.y - self.__v.x * other.__v.z + self.__v.y * other.__s + self.__v.z * other.__v.x
     tempVz = self.__s * other.__v.z + self.__v.x + other.__v.y - self.__v.y * other.__v.x + self.__v.z * other.__s
     return Quaternion(tempS, Point3D(tempVx, tempVy, tempVz))
Ejemplo n.º 6
0
def matrixToQuaternion(xMatrix):
    # 由矩阵到四元数的转换
    assert isinstance(xMatrix, Matrix3D)
    tempQ0 = (xMatrix.trace() + 1)**0.5 / 2
    tempQ1 = (xMatrix[1][2] - xMatrix[2][1]) / (4 * tempQ0)
    tempQ2 = (xMatrix[2][0] - xMatrix[0][2]) / (4 * tempQ0)
    tempQ3 = (xMatrix[0][1] - xMatrix[1][0]) / (4 * tempQ0)
    return Quaternion(tempQ0, Point3D(tempQ1, tempQ2, tempQ3))
Ejemplo n.º 7
0
def crossMultiply(xPoint1, xPoint2):
    if isinstance(xPoint1, Point3D) and isinstance(xPoint2, Point3D):
        # 三维向量叉乘,求同时垂直于两个向量的向量
        tempX = xPoint1.y * xPoint2.z - xPoint1.z * xPoint2.y
        tempY = xPoint1.x * xPoint2.z - xPoint1.z * xPoint2.x
        tempZ = xPoint1.x * xPoint2.y - xPoint1.y * xPoint2.x
        return Point3D(tempX, tempY, tempZ)
    else:
        return None
Ejemplo n.º 8
0
def dotMul(xQuaternion: Quaternion, yQuaternion: Quaternion):
    # 点乘
    assert isinstance(xQuaternion, Quaternion) and isinstance(
        yQuaternion, Quaternion)
    tempS = xQuaternion.s * yQuaternion.s
    tempVx = xQuaternion.v.x * yQuaternion.v.x
    tempVy = xQuaternion.v.y * yQuaternion.v.y
    tempVz = xQuaternion.v.z * yQuaternion.v.z
    return Quaternion(tempS, Point3D(tempVx, tempVy, tempVz))
Ejemplo n.º 9
0
    def __init__(self, xFacet=Point3D(), xVertex=Triangle()):
        """
        三角面片初始化函数

        :param xFacet: 法向量
        :param xVertex: 顶点(3个)
        """
        self.__facet = deepcopy(xFacet)
        self.__vertex = deepcopy(xVertex)
Ejemplo n.º 10
0
 def __init__(self, xOrigin=Point3D(), xDirection=Point3D(1, 1, 0)):
     assert isinstance(xOrigin, Point3D) and isinstance(xDirection, Point3D)
     self.__origin = deepcopy(xOrigin)
     self.__direction = deepcopy(xDirection)
Ejemplo n.º 11
0
    assert isinstance(xMatrix, Matrix3D)
    tempQ0 = (xMatrix.trace() + 1)**0.5 / 2
    tempQ1 = (xMatrix[1][2] - xMatrix[2][1]) / (4 * tempQ0)
    tempQ2 = (xMatrix[2][0] - xMatrix[0][2]) / (4 * tempQ0)
    tempQ3 = (xMatrix[0][1] - xMatrix[1][0]) / (4 * tempQ0)
    return Quaternion(tempQ0, Point3D(tempQ1, tempQ2, tempQ3))


def quaternionToMatrix(xQuaternion):
    # 由四元数到矩阵的转换
    assert isinstance(xQuaternion, Quaternion)
    tempQ0 = xQuaternion.s
    tempQ1 = xQuaternion.v.x
    tempQ2 = xQuaternion.v.y
    tempQ3 = xQuaternion.v.z
    R00 = 1 - 2 * tempQ2**2 - 2 * tempQ3**2
    R01 = 2 * tempQ1 * tempQ2 + 2 * tempQ0 * tempQ3
    R02 = 2 * tempQ1 * tempQ3 - 2 * tempQ0 * tempQ2
    R10 = 2 * tempQ1 * tempQ2 - 2 * tempQ0 * tempQ3
    R11 = 1 - 2 * tempQ1**2 - 2 * tempQ3**2
    R12 = 2 * tempQ2 * tempQ3 + 2 * tempQ0 * tempQ1
    R20 = 2 * tempQ1 * tempQ3 + 2 * tempQ0 * tempQ2
    R21 = 2 * tempQ2 * tempQ3 - 2 * tempQ0 * tempQ1
    R22 = 1 - 2 * tempQ1**2 - 2 * tempQ2**2
    return Matrix3D([[R00, R01, R02], [R10, R11, R12], [R20, R21, R22]])


if __name__ == '__main__':
    testQuaternion = Quaternion(1, Point3D(1, 2, 3))
    print(testQuaternion)
Ejemplo n.º 12
0
    @property
    def vertex2(self):
        return self.__vertex2

    @property
    def vertex3(self):
        return self.__vertex3

    @vertex1.setter
    def vertex1(self, xVertex1):
        self.__vertex1 = deepcopy(xVertex1)

    @vertex2.setter
    def vertex2(self, xVertex2):
        self.__vertex2 = deepcopy(xVertex2)

    @vertex3.setter
    def vertex3(self, xVertex3):
        self.__vertex3 = deepcopy(xVertex3)

    def __str__(self):
        return '[%s,%s,%s]' % (self.__vertex1, self.__vertex2, self.__vertex3)


if __name__ == '__main__':
    testPoint1 = Point3D(0, 0, 0)
    testPoint2 = Point3D(1, 0, 0)
    testPoint3 = Point3D(0, 1, 0)
    testTriangle = Triangle(testPoint1, testPoint2, testPoint3)
    print(testTriangle)
Ejemplo n.º 13
0
    @property
    def point1(self):
        return self.__point1

    @property
    def point2(self):
        return self.__point2

    @point0.setter
    def point0(self, xPoint):
        self.__point0 = xPoint

    @point1.setter
    def point1(self, xPoint):
        self.__point1 = xPoint

    @point2.setter
    def point2(self, xPoint):
        self.__point2 = xPoint

    def __str__(self):
        return '[%s,\n %s,\n %s]' % (self.__point0, self.__point1, self.__point2)


if __name__ == '__main__':
    pt1 = Point3D(1, 1, 1)
    pt2 = Point3D(1, 2, 2)
    pt3 = Point3D(1, 2, 1)
    ttt = Plane(pt1, pt2, pt3)
    print(ttt.normVector())