Esempio n. 1
0
 def _compute_normals(self, model):
     XYZ = np.array([
         model.elements[0].data['x'], model.elements[0].data['y'],
         model.elements[0].data['z']
     ]).transpose()
     N = pptk.estimate_normals(XYZ.astype('float64'), k=30, r=np.inf)
     return N
    def test_subsample(self):
        y = np.zeros((10, 3), dtype=np.float64)
        y[:, 0] = np.arange(10)
        k = 4
        r = 3
        evals = np.array([2. / 3] + [5. / 4] * 8 + [2. / 3])
        evecs = np.array([[1., 0., 0.]] * 10)
        nbhd_sizes = np.array([3] + [4] * 8 + [3])

        s = [0, 3, 4, -1]

        mask = np.zeros(10, dtype=np.bool)
        mask[s] = True
        z = pptk.estimate_normals(y,
                                  k,
                                  r,
                                  subsample=mask,
                                  output_eigenvalues=True,
                                  output_all_eigenvectors=True,
                                  output_neighborhood_sizes=True,
                                  verbose=False)
        self.assertTrue(np.allclose(z[0][:, -1, :], evecs[s]))
        self.assertTrue(np.allclose(z[1][:, -1], evals[s]))
        self.assertTrue(np.all(z[2] == nbhd_sizes[s]))

        mask = np.zeros(2, dtype=np.bool)
        with self.assertRaises(ValueError):
            pptk.estimate_normals(y,
                                  k,
                                  r,
                                  subsample=mask,
                                  output_eigenvalues=True,
                                  output_all_eigenvectors=True,
                                  output_neighborhood_sizes=True,
                                  verbose=False)

        z = pptk.estimate_normals(y,
                                  k,
                                  r,
                                  subsample=s,
                                  output_eigenvalues=True,
                                  output_all_eigenvectors=True,
                                  output_neighborhood_sizes=True,
                                  verbose=False)
        self.assertTrue(np.allclose(z[0][:, -1, :], evecs[s]))
        self.assertTrue(np.allclose(z[1][:, -1], evals[s]))
        self.assertTrue(np.all(z[2] == nbhd_sizes[s]))
def computePCFeatures(selection, points, colors, knn=6, radius=np.inf):
    normals = pptk.estimate_normals(points[selection], knn, radius)
    idx_ground = np.where(points[..., 2] > np.min(points[..., 2] + 0.3))
    idx_normals = np.where(abs(normals[..., 2]) < 0.9)
    idx_wronglyfiltered = np.setdiff1d(idx_ground, idx_normals)
    common_filtering = np.append(idx_normals, idx_wronglyfiltered)
    return pptk.viewer(points[common_filtering],
                       colors[common_filtering] / 65535)
Esempio n. 4
0
    def curvature(self, points=None, k=100, r=0.35):
        """
        This function takes in a set of points (or uses the currently rendered points) and calculates the surface
        curvature using pptk built-in functions which use PCA method. The number of neighbors (k) or the distance
        scale (r) can be changed to affect the resolution of the computation. If render=True, then the results
        will be rendered upon completion.
        """
        if points is None:
            points = self.points.loc[self.showing.bools][['x', 'y', 'z']].values

        eigens = np.abs(pptk.estimate_normals(points, k, r, output_eigenvalues=True)[0])
        eigens.sort(axis=1)
        return eigens[:, 0] / eigens.sum(axis=1) * 3.0
Esempio n. 5
0
    def normals(self, points=None, k=100, r=0.35, render=False):
        """
        This function takes in a set of points (or uses the currently rendered points) and calculates the surface
        normals using pptk built-in functions which use PCA method. The number of neighbors (k) or the distance
        scale (r) can be changed to affect the resolution of the computation. If render=True, then the results
        will be rendered upon completion.
        """
        if points is None:
            points = self.points.loc[self.showing.bools][['x', 'y', 'z']].values

        n = np.abs(pptk.estimate_normals(points, k, r))
        if render:
            self.viewer.attributes(n)

        return n
    def test_rand100(self):
        np.random.seed(0)
        x = pptk.rand(100, 3)

        pptk.estimate_normals(x, 5, 0.2, verbose=False)
        pptk.estimate_normals(x,
                              5,
                              0.2,
                              output_eigenvalues=True,
                              verbose=False)
        pptk.estimate_normals(x,
                              5,
                              0.2,
                              output_all_eigenvectors=True,
                              verbose=False)
        pptk.estimate_normals(x,
                              5,
                              0.2,
                              output_eigenvalues=True,
                              output_all_eigenvectors=True,
                              verbose=False)

        x = np.float32(x)
        pptk.estimate_normals(x, 5, 0.2, verbose=False)
        pptk.estimate_normals(x,
                              5,
                              0.2,
                              output_eigenvalues=True,
                              verbose=False)
        pptk.estimate_normals(x,
                              5,
                              0.2,
                              output_all_eigenvectors=True,
                              verbose=False)
        pptk.estimate_normals(x,
                              5,
                              0.2,
                              output_eigenvalues=True,
                              output_all_eigenvectors=True,
                              verbose=False)
    def test_output_types(self):
        # test all 8 combinations of output_* switches

        y = np.zeros((10, 3), dtype=np.float64)
        y[:, 0] = np.arange(10)
        k = 4
        r = 3
        evals = np.array([2. / 3] + [5. / 4] * 8 + [2. / 3])
        evecs = np.array([[1., 0., 0.]] * 10)
        nbhd_sizes = np.array([3] + [4] * 8 + [3])

        # combo 1: (0, 0, 0)
        z = pptk.estimate_normals(y,
                                  k,
                                  r,
                                  output_eigenvalues=False,
                                  output_all_eigenvectors=False,
                                  output_neighborhood_sizes=False,
                                  verbose=False)
        self.assertTrue(isinstance(z, np.ndarray) and z.shape == (10, 3))

        # combo 2: (0, 0, 1)
        z = pptk.estimate_normals(y,
                                  k,
                                  r,
                                  output_eigenvalues=False,
                                  output_all_eigenvectors=False,
                                  output_neighborhood_sizes=True,
                                  verbose=False)
        self.assertTrue(
            isinstance(z, tuple) and len(z) == 3 and [type(i) for i in z]
            == [np.ndarray, type(None), np.ndarray] and z[0].shape == (10, 3)
            and z[2].shape == (10, ))
        self.assertTrue(np.all(z[2] == nbhd_sizes))

        # combo 3: (0, 1, 0)
        z = pptk.estimate_normals(y,
                                  k,
                                  r,
                                  output_eigenvalues=False,
                                  output_all_eigenvectors=True,
                                  output_neighborhood_sizes=False,
                                  verbose=False)
        self.assertTrue(isinstance(z, np.ndarray) and z.shape == (10, 3, 3))

        # combo 4: (0, 1, 1)
        z = pptk.estimate_normals(y,
                                  k,
                                  r,
                                  output_eigenvalues=False,
                                  output_all_eigenvectors=True,
                                  output_neighborhood_sizes=True,
                                  verbose=False)
        self.assertTrue(
            isinstance(z, tuple) and len(z) == 3
            and [type(i) for i in z] == [np.ndarray,
                                         type(None), np.ndarray]
            and z[0].shape == (10, 3, 3) and z[2].shape == (10, ))
        self.assertTrue(np.allclose(z[0][:, -1, :], evecs))
        self.assertTrue(np.all(z[2] == nbhd_sizes))

        # combo 5: (1, 0, 0)
        z = pptk.estimate_normals(y,
                                  k,
                                  r,
                                  output_eigenvalues=True,
                                  output_all_eigenvectors=False,
                                  output_neighborhood_sizes=False,
                                  verbose=False)
        self.assertTrue(
            isinstance(z, tuple) and len(z) == 3
            and [type(i) for i in z] == [np.ndarray, np.ndarray,
                                         type(None)] and z[0].shape == (10, 3)
            and z[1].shape == (10, ))

        # combo 6: (1, 0, 1)
        z = pptk.estimate_normals(y,
                                  k,
                                  r,
                                  output_eigenvalues=True,
                                  output_all_eigenvectors=False,
                                  output_neighborhood_sizes=True,
                                  verbose=False)
        self.assertTrue(
            isinstance(z, tuple) and len(z) == 3
            and [type(i) for i in z] == [np.ndarray, np.ndarray, np.ndarray]
            and [i.shape for i in z] == [(10, 3), (10, ), (10, )])
        self.assertTrue(np.all(z[2] == nbhd_sizes))

        # combo 7: (1, 1, 0)
        z = pptk.estimate_normals(y,
                                  k,
                                  r,
                                  output_eigenvalues=True,
                                  output_all_eigenvectors=True,
                                  output_neighborhood_sizes=False,
                                  verbose=False)
        self.assertTrue(
            isinstance(z, tuple) and len(z) == 3
            and [type(i) for i in z] == [np.ndarray, np.ndarray,
                                         type(None)]
            and z[0].shape == (10, 3, 3) and z[1].shape == (10, 3))
        self.assertTrue(np.allclose(z[0][:, -1, :], evecs))
        self.assertTrue(np.allclose(z[1][:, -1], evals))

        # combo 8: (1, 1, 1)
        z = pptk.estimate_normals(y,
                                  k,
                                  r,
                                  output_eigenvalues=True,
                                  output_all_eigenvectors=True,
                                  output_neighborhood_sizes=True,
                                  verbose=False)
        self.assertTrue(
            isinstance(z, tuple) and len(z) == 3
            and [type(i) for i in z] == [np.ndarray, np.ndarray, np.ndarray]
            and [i.shape for i in z] == [(10, 3, 3), (10, 3), (10, )])
        self.assertTrue(np.allclose(z[0][:, -1, :], evecs))
        self.assertTrue(np.allclose(z[1][:, -1], evals))
        self.assertTrue(np.all(z[2] == nbhd_sizes))