Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

''' Routines and Fitting algorithms 

   Fitting: means where ALL other non-object points 

   have been removed, determining the transform frame 

   of the object 

 

   Segment: means seperating clusters from a single cloud 

''' 

 

 

from director.filterUtils import * 

import director.visualization as vis 

from director import objectmodel as om 

from director.transformUtils import getTransformFromAxes 

from director import vtkAll as vtk 

 

import vtkNumpy 

import numpy as np 

from shallowCopy import shallowCopy 

from debugVis import DebugData 

 

 

 

class SegmentationContext(object): 

    ''' 

       Maintains an abstraction between the fitting scene and a robot 

       Assumes point cloud is world aligned, with z up 

       Provides access to (1) ground height, 

       (2) location of the head frame, (3) view direction 

 

       Can be configured: 

       (a) Default mode: populated continously by EST_ROBOT_STATE 

           (2) and (3) set seperately 

       (b) Autonomy: where (2) gives (3) 

       (c) Populated programmatically. e.g: 

           - for unit testing 

           - where ground plane from feet cannot be used 

    ''' 

 

    def __init__(self, groundHeightProvider, viewProvider): 

        self.groundHeightProvider = groundHeightProvider 

        self.viewProvider = viewProvider 

 

    def getGroundHeight(self): 

        return self.groundHeightProvider.getGroundHeight() 

 

    def getViewFrame(self): 

        return self.viewProvider.getViewFrame() 

 

    def getViewOrigin(self): 

        return self.viewProvider.getViewOrigin() 

 

    def getViewDirection(self): 

        return self.viewProvider.getViewDirection() 

 

    ''' 

    These static methods are provided for convenience to initialize 

    a globalally accessible instance of the SegmentationContext. 

    ''' 

 

    _globalSegmentationContext = None 

 

    @staticmethod 

    def installGlobalInstance(inst): 

        if SegmentationContext._globalSegmentationContext is not None: 

            raise Exception('Error, a global segmentation context instance is already installed.') 

 

        SegmentationContext._globalSegmentationContext = inst 

 

    @staticmethod 

    def getGlobalInstance(): 

        if SegmentationContext._globalSegmentationContext is None: 

            raise Exception('Error, the global segmentation context instance has not been initialized.') 

        return SegmentationContext._globalSegmentationContext 

 

    @staticmethod 

    def initWithRobot(model): 

        sc = SegmentationContext(RobotModelGroundHeightProvider(model), RobotModelViewProvider(model)) 

        SegmentationContext.installGlobalInstance(sc) 

 

    @staticmethod 

    def initWithCamera(camera, userGroundHeight): 

        sc = SegmentationContext(UserGroundHeightProvider(userGroundHeight), CameraViewProvider(camera)) 

        SegmentationContext.installGlobalInstance(sc) 

 

    @staticmethod 

    def initWithUser(userGroundHeight, userViewFrame, viewAxis=0): 

        sc = SegmentationContext(UserGroundHeightProvider(userGroundHeight), UserViewProvider(userViewFrame, viewAxis)) 

        SegmentationContext.installGlobalInstance(sc) 

 

 

class RobotModelGroundHeightProvider(object): 

 

    def __init__(self, model): 

        self.model = model 

 

    def getGroundHeight(self): 

        from director.footstepsdriver import FootstepsDriver 

        return FootstepsDriver.getFeetMidPoint(self.model).GetPosition()[2] 

 

 

class RobotModelViewProvider(object): 

 

    def __init__(self, model): 

        self.model = model 

 

    def getViewFrame(self): 

        return self.model.getLinkFrame(self.model.getHeadLink()) 

 

    def getViewOrigin(self): 

        headFrame = self.model.getLinkFrame(self.model.getHeadLink()) 

        return np.array(headFrame.GetPosition()) 

 

    def getViewDirection(self): 

        headFrame = self.model.getLinkFrame(self.model.getHeadLink()) 

        viewDirection = [1,0,0] 

        headFrame.TransformVector(viewDirection, viewDirection) 

        return np.array(viewDirection) 

 

class UserGroundHeightProvider(object): 

 

    def __init__(self, groundHeight): 

        self.groundHeight = groundHeight 

 

    def getGroundHeight(): 

        return self.groundHeight 

 

class UserViewProvider(object): 

 

    def __init__(self, viewFrame, viewAxis): 

        self.viewFrame = viewFrame 

        self.viewAxis = viewAxis 

 

    def getViewFrame(self): 

        return self.viewFrame 

 

    def getViewOrigin(self): 

        return np.array( self.viewFrame.GetPosition()) 

 

    def getViewDirection(self): 

        viewDirection = [0.0, 0.0, 0.0] 

        viewDirection[self.viewAxis] = 1.0 

        self.viewFrame.TransformVector(viewDirection, viewDirection) 

        return np.array(viewDirection) 

 

class CameraViewProvider(object): 

 

    def __init__(self, camera): 

        self.camera = camera 

 

    def getViewFrame(self): 

        return  self.camera.GetViewTransformObject() 

 

    def getViewOrigin(self): 

        return np.array(self.camera.GetViewPosition()) 

 

    def getViewDirection(self): 

        return np.array(self.camera.GetViewDirection()) 

 

 

 

def getDebugFolder(): 

    obj = om.findObjectByName('debug') 

    if obj is None: 

        obj = om.getOrCreateContainer('debug', om.getOrCreateContainer('segmentation')) 

        om.collapse(obj) 

    return obj 

 

 

def applyLineFit(dataObj, distanceThreshold=0.02): 

 

    f = vtk.vtkPCLSACSegmentationLine() 

    f.SetInput(dataObj) 

    f.SetDistanceThreshold(distanceThreshold) 

    f.Update() 

    origin = np.array(f.GetLineOrigin()) 

    direction = np.array(f.GetLineDirection()) 

 

    return origin, direction, shallowCopy(f.GetOutput()) 

 

 

def projectPointToPlane(point, origin, normal): 

    projectedPoint = np.zeros(3) 

    vtk.vtkPlane.ProjectPoint(point, origin, normal, projectedPoint) 

    return projectedPoint 

 

 

def intersectLineWithPlane(line_point, line_ray, plane_point, plane_normal ): 

    ''' 

    Find the intersection between a line and a plane 

    http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-7-intersecting-simple-shapes/ray-plane-and-ray-disk-intersection/ 

    ''' 

 

    line_point = np.asarray(line_point) 

    line_ray = np.asarray(line_ray) 

    plane_point = np.asarray(plane_point) 

    plane_normal = np.asarray(plane_normal) 

 

    denom = np.dot( plane_normal , line_ray ) 

 

    # TODO: implement this check 

    #if (denom > 1E-6): 

    #    # ray is very close to parallel to plane 

    #    return None 

 

    p0l0 = plane_point - line_point 

    t = np.dot(p0l0, plane_normal) / denom 

 

    intersection_point = line_point + t*line_ray 

    return intersection_point 

 

 

def labelPointDistanceAlongAxis(polyData, axis, origin=None, resultArrayName='distance_along_axis'): 

 

    points = vtkNumpy.getNumpyFromVtk(polyData, 'Points') 

    if origin is not None: 

        points = points - origin 

    distanceValues = np.dot(points, axis) 

    if origin is None: 

        distanceValues -= np.nanmin(distanceValues) 

    newData = shallowCopy(polyData) 

    vtkNumpy.addNumpyToVtk(newData, distanceValues, resultArrayName) 

    return newData 

 

 

def applyEuclideanClustering(dataObj, clusterTolerance=0.05, minClusterSize=100, maxClusterSize=1e6): 

 

    f = vtk.vtkPCLEuclideanClusterExtraction() 

    f.SetInput(dataObj) 

    f.SetClusterTolerance(clusterTolerance) 

    f.SetMinClusterSize(int(minClusterSize)) 

    f.SetMaxClusterSize(int(maxClusterSize)) 

    f.Update() 

    return shallowCopy(f.GetOutput()) 

 

 

def extractClusters(polyData, clusterInXY=False, **kwargs): 

    ''' Segment a single point cloud into smaller clusters 

        using Euclidean Clustering 

     ''' 

 

    if not polyData.GetNumberOfPoints(): 

        return [] 

 

    if (clusterInXY == True): 

        ''' If Points are seperated in X&Y, then cluster outside this ''' 

        polyDataXY = vtk.vtkPolyData() 

        polyDataXY.DeepCopy(polyData) 

        points=vtkNumpy.getNumpyFromVtk(polyDataXY , 'Points') # shared memory 

        points[:,2] = 0.0 

        #showPolyData(polyDataXY, 'polyDataXY', visible=False, parent=getDebugFolder()) 

        polyDataXY = applyEuclideanClustering(polyDataXY, **kwargs) 

        clusterLabels = vtkNumpy.getNumpyFromVtk(polyDataXY, 'cluster_labels') 

        vtkNumpy.addNumpyToVtk(polyData, clusterLabels, 'cluster_labels') 

 

    else: 

        polyData = applyEuclideanClustering(polyData, **kwargs) 

        clusterLabels = vtkNumpy.getNumpyFromVtk(polyData, 'cluster_labels') 

 

 

    clusters = [] 

    for i in xrange(1, clusterLabels.max() + 1): 

        cluster = thresholdPoints(polyData, 'cluster_labels', [i, i]) 

        clusters.append(cluster) 

    return clusters 

 

 

def applyVoxelGrid(polyData, leafSize=0.01): 

 

    v = vtk.vtkPCLVoxelGrid() 

    v.SetLeafSize(leafSize, leafSize, leafSize) 

    v.SetInput(polyData) 

    v.Update() 

    return shallowCopy(v.GetOutput()) 

 

 

def labelOutliers(dataObj, searchRadius=0.03, neighborsInSearchRadius=10): 

 

    f = vtk.vtkPCLRadiusOutlierRemoval() 

    f.SetInput(dataObj) 

    f.SetSearchRadius(searchRadius) 

    f.SetNeighborsInSearchRadius(int(neighborsInSearchRadius)) 

    f.Update() 

    return shallowCopy(f.GetOutput()) 

 

 

def sparsifyStereoCloud(polyData): 

    ''' Take in a typical Stereo Camera Point Cloud 

    Filter it down to about the density of a lidar point cloud 

    and remove outliers 

    ''' 

 

    # >>> strips color out <<< 

    polyData = applyVoxelGrid(polyData, leafSize=0.01) 

 

    # remove outliers 

    polyData = labelOutliers(polyData) 

    vis.showPolyData(polyData, 'is_outlier', colorByName='is_outlier', visible=False, parent=getDebugFolder()) 

    polyData = thresholdPoints(polyData, 'is_outlier', [0.0, 0.0]) 

    return polyData 

 

def fitDrillBarrel ( drillPoints, forwardDirection, plane_origin, plane_normal): 

    ''' Given a point cloud which ONLY contains points from a barrell drill, standing upright 

        and the equations of a table its resting on, and the general direction of the robot 

        Fit a barrell drill 

     ''' 

 

    if not drillPoints.GetNumberOfPoints(): 

        return 

 

    vis.updatePolyData(drillPoints, 'drill cluster', parent=getDebugFolder(), visible=False) 

    drillBarrelPoints = thresholdPoints(drillPoints, 'dist_to_plane', [0.177, 0.30]) 

 

    if not drillBarrelPoints.GetNumberOfPoints(): 

        return 

 

 

    # fit line to drill barrel points 

    linePoint, lineDirection, _ = applyLineFit(drillBarrelPoints, distanceThreshold=0.5) 

 

    if np.dot(lineDirection, forwardDirection) < 0: 

        lineDirection = -lineDirection 

 

    vis.updatePolyData(drillBarrelPoints, 'drill barrel points', parent=getDebugFolder(), visible=False) 

 

 

    pts = vtkNumpy.getNumpyFromVtk(drillBarrelPoints, 'Points') 

 

    dists = np.dot(pts-linePoint, lineDirection) 

 

    p1 = linePoint + lineDirection*np.min(dists) 

    p2 = linePoint + lineDirection*np.max(dists) 

 

    p1 = projectPointToPlane(p1, plane_origin, plane_normal) 

    p2 = projectPointToPlane(p2, plane_origin, plane_normal) 

 

 

    d = DebugData() 

    d.addSphere(p1, radius=0.01) 

    d.addSphere(p2, radius=0.01) 

    d.addLine(p1, p2) 

    vis.updatePolyData(d.getPolyData(), 'drill debug points', color=[0,1,0], parent=getDebugFolder(), visible=False) 

 

 

    drillToBasePoint = np.array([-0.07,  0.0  , -0.12]) 

 

    zaxis = plane_normal 

    xaxis = lineDirection 

    xaxis /= np.linalg.norm(xaxis) 

    yaxis = np.cross(zaxis, xaxis) 

    yaxis /= np.linalg.norm(yaxis) 

    xaxis = np.cross(yaxis, zaxis) 

    xaxis /= np.linalg.norm(xaxis) 

 

    t = getTransformFromAxes(xaxis, yaxis, zaxis) 

    t.PreMultiply() 

    t.Translate(-drillToBasePoint) 

    t.PostMultiply() 

    t.Translate(p1) 

 

    return t