-
Notifications
You must be signed in to change notification settings - Fork 27
/
MoMo.scala
819 lines (710 loc) · 29.9 KB
/
MoMo.scala
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/*
* Copyright University of Basel, Graphics and Vision Research Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package scalismo.faces.momo
import breeze.linalg.{DenseMatrix, DenseVector}
import scalismo.common.{DiscreteField, PointId, UnstructuredPointsDomain}
import scalismo.color.{RGB, RGBA}
import scalismo.mesh.VertexColorMesh3D
import scalismo.geometry._
import scalismo.mesh.{TriangleMesh3D, _}
import scalismo.statisticalmodel._
import scalismo.statisticalmodel.dataset.DataCollection
import scalismo.utils.Random
/** 3D Morphable Model with shape, color and expressions */
trait MoMo {
/** reference of Morphable Model, defines registration and triangulation */
def referenceMesh: TriangleMesh3D
/** all landmarks defined on this model, points on reference mesh */
def landmarks: Map[String, Landmark[_3D]] = Map.empty[String, Landmark[_3D]]
/** returns true if this model has a non-empty expression part */
def hasExpressions: Boolean
/** construct the neutral (identity) model without expressions */
def neutralModel: MoMoBasic
def expressionModel: Option[MoMoExpress]
/**
* Returns the mean VertexColorMesh3D of the model.
*
* @return
* the mean
*/
def mean: VertexColorMesh3D
/**
* Generate model instance described by coefficients.
*
* @param coefficients
* model coefficients
* @return
* model instance as mesh with per vertex color
*/
def instance(coefficients: MoMoCoefficients): VertexColorMesh3D
/**
* A fast method to evaluate a model instance at a specific point.
*
* @param coefficients
* model coefficients
* @param pid
* point-id
* @return
* shape and color values at point-id
*/
def instanceAtPoint(coefficients: MoMoCoefficients, pid: PointId): (Point[_3D], RGB)
/**
* Calculates the parameters of the model representation of the sample.
*
* @param sample
* the sample (shape and color)
* @return
* the model coefficients
*/
def coefficients(sample: VertexColorMesh3D): MoMoCoefficients
/**
* Projection operator in model space.
*
* @param sample
* the sample (shape and color)
* @return
* the model reconstruction of the sample
*/
def project(sample: VertexColorMesh3D): VertexColorMesh3D = instance(coefficients(sample))
/**
* Draw a sample from the model.
*
* @return
* a sample (shape and color)
*/
def sample()(implicit rnd: Random): VertexColorMesh3D
/**
* Draw a set of coefficients from the prior of the statistical model.
*
* @return
* a set of coefficients to generate a random sample.
*/
def sampleCoefficients()(implicit rnd: Random): MoMoCoefficients
/**
* Returns the same model but with exchanged or added landmarks.
*
* @param landmarksMap
* Map of named landmarks.
* @return
* Same model but with exchanged landmarks.
*/
def withLandmarks(landmarksMap: Map[String, Landmark[_3D]]): MoMo
/**
* Test if the model has some landmarks.
*
* @return
* true if the model has at least one landmark, false otherwise
*/
def hasLandmarks: Boolean = landmarks.nonEmpty
/**
* Returns the PointId for a given landmark name.
*
* @param id
* name ot the landmark
* @return
* return the PointId of the landmark
*/
def landmarkPointId(id: String): Option[PointId] = {
for {
lm <- landmarks.get(id)
id <- referenceMesh.pointSet.pointId(lm.point)
} yield id
}
/** get all landmarks expressed thruogh PointIds */
def landmarksWithPointIds: Map[String, Option[PointId]] = landmarks.map { case (id, lm) =>
id -> referenceMesh.pointSet.pointId(lm.point)
}
/** get all landmarks expressed thruogh PointIds, use closest point for each landmarks (careful!) */
def landmarksWithClosestPointIds: Map[String, PointId] = landmarks.map { case (id, lm) =>
id -> referenceMesh.pointSet.findClosestPoint(lm.point).id
}
/** pad a coefficient vector if it is too short, basis with single vector */
def padCoefficients(momoCoeff: MoMoCoefficients): MoMoCoefficients
/** get a coefficients vector describing the mean, has proper dimensions */
def zeroCoefficients: MoMoCoefficients
}
object MoMo {
/** create a Morphable Model */
def apply(referenceMesh: TriangleMesh3D,
shape: PancakeDLRGP[_3D, TriangleMesh, Point[_3D]],
color: PancakeDLRGP[_3D, TriangleMesh, RGB],
expression: PancakeDLRGP[_3D, TriangleMesh, EuclideanVector[_3D]],
landmarks: Map[String, Landmark[_3D]]
): MoMoExpress = {
MoMoExpress(referenceMesh, shape, color, expression, landmarks)
}
/** create a Morphable Model without expressions */
def apply(referenceMesh: TriangleMesh3D,
shape: PancakeDLRGP[_3D, TriangleMesh, Point[_3D]],
color: PancakeDLRGP[_3D, TriangleMesh, RGB],
landmarks: Map[String, Landmark[_3D]]
): MoMoBasic = {
MoMoBasic(referenceMesh, shape, color, landmarks)
}
/** create a Morphable Model */
def apply(referenceMesh: TriangleMesh3D,
shape: PancakeDLRGP[_3D, TriangleMesh, Point[_3D]],
color: PancakeDLRGP[_3D, TriangleMesh, RGB],
expression: PancakeDLRGP[_3D, TriangleMesh, EuclideanVector[_3D]]
): MoMoExpress = {
MoMoExpress(referenceMesh, shape, color, expression, Map.empty)
}
/** create a Morphable Model without expressions */
def apply(referenceMesh: TriangleMesh3D,
shape: PancakeDLRGP[_3D, TriangleMesh, Point[_3D]],
color: PancakeDLRGP[_3D, TriangleMesh, RGB]
): MoMoBasic = {
MoMoBasic(referenceMesh, shape, color, Map.empty)
}
/**
* Builds a MoMo from a scalismo StatisticalMeshModel and a color GP.
*
* @param shape
* scalismo.statisticalmodel.StatisticalMeshModel for the shape
* @param color
* DLRGP model for the color
* @return
* New MoMo with the statistics of the two model.
*/
def fromStatisticalMeshModel(shape: StatisticalMeshModel,
color: PancakeDLRGP[_3D, TriangleMesh, RGB],
shapeNoiseVariance: Double = 0.0
): MoMoBasic = {
val shapeModel = PancakeDLRGP(ModelHelpers.vectorToPointDLRGP(shape.gp, shape.referenceMesh), shapeNoiseVariance)
MoMoBasic(shape.referenceMesh, shapeModel, color)
}
/**
* Build 3d Morphable Model from registered samples.
*
* @param reference
* the reference mesh
* @param samplesShape
* shape samples in dense correspondence with reference
* @param samplesColor
* color samples in dense correspondence with reference
* @param shapeNoiseVariance
* spherical noise term in PPCA
* @param colorNoiseVariance
* spherical noise term in PPCA
* @return
*/
def buildFromRegisteredSamples(reference: TriangleMesh3D,
samplesShape: IndexedSeq[VertexColorMesh3D],
samplesColor: IndexedSeq[VertexColorMesh3D],
shapeNoiseVariance: Double,
colorNoiseVariance: Double
): MoMoBasic = {
require(samplesShape.nonEmpty, "MoMo needs shape samples (>0)")
require(samplesColor.nonEmpty, "MoMo needs color samples (>0)")
require(samplesShape.forall(e => e.shape.pointSet.numberOfPoints == reference.pointSet.numberOfPoints),
"MoMo samples must be compatible with reference"
)
require(samplesColor.forall(e => e.shape.pointSet.numberOfPoints == reference.pointSet.numberOfPoints),
"MoMo samples must be compatible with reference"
)
val domain = reference
val shapeSamples = samplesShape.map { (sample: VertexColorMesh3D) =>
DiscreteField[_3D, TriangleMesh, Point[_3D]](domain, sample.shape.pointSet.points.toIndexedSeq)
}
val colorSamples = samplesColor.map { (sample: VertexColorMesh3D) =>
DiscreteField[_3D, TriangleMesh, RGB](domain, sample.color.pointData.map { _.toRGB })
}
val shapeModel =
ModelHelpers.createUsingPPCA[_3D, TriangleMesh, Point[_3D]](domain, shapeSamples, shapeNoiseVariance)
val colorModel = ModelHelpers.createUsingPPCA[_3D, TriangleMesh, RGB](domain, colorSamples, colorNoiseVariance)
MoMo(reference, shapeModel, colorModel)
}
/**
* Keep an expression scan together with its corresponding neutral scan
*/
case class NeutralWithExpression(neutral: VertexColorMesh3D, expression: VertexColorMesh3D)
/**
* Build a 3D Morphable Model with expressions from registered samples.
*
* @param reference
* the reference mesh
* @param samplesShape
* shape samples in dense correspondence with reference
* @param samplesColor
* color samples in dense correspondence with reference
* @param samplesExpression
* expression model samples, consist of a neutral and an expression sample
* @param shapeNoiseVariance
* spherical noise term in PPCA
* @param colorNoiseVariance
* spherical noise term in PPCA
* @param expressionNoiseVariance
* spherical noise term in PPCA
* @return
*/
def buildFromRegisteredSamples(reference: TriangleMesh3D,
samplesShape: IndexedSeq[VertexColorMesh3D],
samplesColor: IndexedSeq[VertexColorMesh3D],
samplesExpression: IndexedSeq[NeutralWithExpression],
shapeNoiseVariance: Double,
colorNoiseVariance: Double,
expressionNoiseVariance: Double
): MoMoExpress = {
require(samplesShape.nonEmpty, "MoMo needs shape samples (>0)")
require(samplesColor.nonEmpty, "MoMo needs color samples (>0)")
require(samplesExpression.nonEmpty, "MoMo needs expression samples (>0)")
require(samplesShape.forall(e => e.shape.pointSet.numberOfPoints == reference.pointSet.numberOfPoints),
"MoMo samples must be compatible with reference"
)
require(samplesColor.forall(e => e.shape.pointSet.numberOfPoints == reference.pointSet.numberOfPoints),
"MoMo samples must be compatible with reference"
)
require(
samplesExpression.forall(e => e.neutral.shape.pointSet.numberOfPoints == reference.pointSet.numberOfPoints),
"Expression/Neutral samples must be compatible with reference"
)
require(
samplesExpression.forall(e => e.expression.shape.pointSet.numberOfPoints == reference.pointSet.numberOfPoints),
"Expression samples must be compatible with reference"
)
val shapeSamples = samplesShape.map { (sample: VertexColorMesh3D) =>
DiscreteField[_3D, TriangleMesh, Point[_3D]](reference, sample.shape.pointSet.points.toIndexedSeq)
}
val colorSamples = samplesColor.map { (sample: VertexColorMesh3D) =>
DiscreteField[_3D, TriangleMesh, RGB](reference, sample.color.pointData.map { _.toRGB })
}
val expressionSamples = samplesExpression.map { case NeutralWithExpression(neutral, exp) =>
val difference = reference.pointSet.pointIds.map { pointId =>
exp.shape.pointSet.point(pointId) - neutral.shape.pointSet.point(pointId)
}
DiscreteField(reference, difference.toIndexedSeq)
}
val shapeModel =
ModelHelpers.createUsingPPCA[_3D, TriangleMesh, Point[_3D]](reference, shapeSamples, shapeNoiseVariance)
val colorModel = ModelHelpers.createUsingPPCA[_3D, TriangleMesh, RGB](reference, colorSamples, colorNoiseVariance)
MoMo(
reference,
shapeModel,
colorModel,
ModelHelpers.createUsingPPCA[_3D, TriangleMesh, EuclideanVector[_3D]](reference,
expressionSamples,
expressionNoiseVariance
)
)
}
}
/**
* 3d Morphable Model implementation, includes facial expression
*
* The model consists of a shape and color model together with noise estimates. Both models are defined over points of
* the reference Mesh. The models are spherical PPCA models.
*
* @param referenceMesh
* reference of the model
* @param shape
* the shape model
* @param color
* the color model
*/
case class MoMoExpress(override val referenceMesh: TriangleMesh3D,
shape: PancakeDLRGP[_3D, TriangleMesh, Point[_3D]],
color: PancakeDLRGP[_3D, TriangleMesh, RGB],
expression: PancakeDLRGP[_3D, TriangleMesh, EuclideanVector[_3D]],
override val landmarks: Map[String, Landmark[_3D]]
) extends MoMo {
require(shape.domain == color.domain, "shape and color model do not have a matching domain")
require(referenceMesh == shape.domain, "reference domain does not match model domain")
require(referenceMesh == expression.domain, "expression model does not have a matching domain")
def hasExpressions: Boolean = true
override lazy val neutralModel: MoMoBasic = MoMoBasic(referenceMesh, shape, color, landmarks)
override def expressionModel: Option[MoMoExpress] = Some(this)
/**
* Returns the mean VertexColorMesh3D of the model.
*
* @return
* the mean
*/
def mean: VertexColorMesh3D = {
VertexColorMesh3D(
discreteFieldToShape(shape.mean, expression.mean),
discreteFieldToColor(color.mean)
)
}
/**
* Generate model instance described by coefficients.
*
* @param coefficients
* model coefficients
* @return
* model instance as mesh with per vertex color
*/
def instance(coefficients: MoMoCoefficients): VertexColorMesh3D = {
val MoMoCoefficients(shapeCoefficients, colorCoefficients, expressCoefficients) = padCoefficients(coefficients)
VertexColorMesh3D(
discreteFieldToShape(shape.instance(shapeCoefficients), expression.instance(expressCoefficients)),
discreteFieldToColor(color.instance(colorCoefficients))
)
}
/**
* A fast method to evaluate a model instance at a specific point.
*
* @param coefficients
* model coefficients
* @param pid
* point-id
* @return
* shape and color values at point-id
*/
def instanceAtPoint(coefficients: MoMoCoefficients, pid: PointId): (Point[_3D], RGB) = {
val MoMoCoefficients(shapeCoefficients, colorCoefficients, expressCoefficients) = padCoefficients(coefficients)
val point = shape.instanceAtPoint(shapeCoefficients, pid) + expression.instanceAtPoint(expressCoefficients, pid)
val colorAtPoint = color.instanceAtPoint(colorCoefficients, pid)
(point, colorAtPoint)
}
/**
* Calculates the parameters of the model representation of the sample.
*
* @param sample
* the sample (shape and color)
* @return
* the model coefficients
*/
override def coefficients(sample: VertexColorMesh3D): MoMoCoefficients = {
require(sample.shape.pointSet.numberOfPoints == referenceMesh.pointSet.numberOfPoints,
"mesh to project does not have the same amount of points as model"
)
// color
val colorCoeffs = color.coefficients(colorToDiscreteField(sample.color))
// composite shape with precalculation of matrices
// u = f(alpha_N, alpha_E) = mu_N + mu_E + M_N * alpha_N + M_E * alpha_E + epsilon_N + epsilon_N
// mu_alpha|u = ( (sigma_N^2 + sigma_E^2)*I + W_tilde )^-1 * [W_N | W_E]^T * (u - mu_N - mu_E)
// W_tilde = [ W_N^T * W_N W_N^T * W_E ; W_E^T * W_N W_E^T * W_E ]
// sigma^2 = [ sigma_N^2 sigma_E^2 ]
// sigma_N^2: number of elements corresponding to number of columns of M_N
// sigma_E^2: number of elements corresponding to number of columns of M_E
val pointIds = sample.shape.pointSet.pointIds.toIndexedSeq
val vertexDim = shape.vectorizer.dim
val shapeVectorLength = vertexDim * referenceMesh.pointSet.numberOfPoints
val shapeMu = DenseVector(shape.mean.data.toArray)
val expressMu = DenseVector(expression.mean.data.toArray)
val sampleVec = DenseVector.zeros[Double](shapeVectorLength)
val shapeMuVec = DenseVector.zeros[Double](shapeVectorLength)
val expressMuVec = DenseVector.zeros[Double](shapeVectorLength)
// generates vectors from data (calls vectorizer for every point on reference.)
pointIds.map { pointId =>
val value = sample.shape.pointSet.point(pointId)
val range = pointId.id * vertexDim until (pointId.id + 1) * vertexDim
val vV: DenseVector[Double] = shape.vectorizer.vectorize(value)
sampleVec(range) := vV
val sM: DenseVector[Double] = shape.vectorizer.vectorize(shapeMu(pointId.id))
shapeMuVec(range) := sM
val sE: DenseVector[Double] = expression.vectorizer.vectorize(expressMu(pointId.id))
expressMuVec(range) := sE
}
val n_S = shape.rank
val n_E = expression.rank
val coeffs_SE =
coefficientsMatrices.wTildeNoiseInv * coefficientsMatrices.wT * (sampleVec - shapeMuVec - expressMuVec)
val shapeCoeffs = coeffs_SE(0 until n_S)
val expressCoeffs = coeffs_SE(n_S until n_E + n_S)
MoMoCoefficients(shapeCoeffs, colorCoeffs, expressCoeffs)
}
private case class CoeffsMatrices(wTildeNoiseInv: DenseMatrix[Double], wT: DenseMatrix[Double])
private lazy val coefficientsMatrices: CoeffsMatrices = {
// composite shape
// u = f(alpha_N, alpha_E) = mu_N + mu_E + M_N * alpha_N + M_E * alpha_E + epsilon_N + epsilon_N
// mu_alpha|u = ( (sigma_N^2 + sigma_E^2)*I + W_tilde )^-1 * [W_N | W_E]^T * (u - mu_N - mu_E)
// W_tilde = [ W_N^T * W_N W_N^T * W_E ; W_E^T * W_N W_E^T * W_E ]
// sigma^2 = [ sigma_N^2 sigma_E^2 ]
// sigma_N^2: number of elements corresponding to number of columns of M_N
// sigma_E^2: number of elements corresponding to number of columns of M_E
val pointIds = referenceMesh.pointSet.pointIds.toIndexedSeq
val vertexDim = shape.vectorizer.dim
val shapeVectorLength = vertexDim * referenceMesh.pointSet.numberOfPoints
val shapeBasis = shape.basisMatrixScaled
val expressBasis = expression.basisMatrixScaled
val shapeMu = DenseVector(shape.mean.data.toArray)
val expressMu = DenseVector(expression.mean.data.toArray)
val shapeMuVec = DenseVector.zeros[Double](shapeVectorLength)
val expressMuVec = DenseVector.zeros[Double](shapeVectorLength)
// generates vectors from data (calls vectorizer for every point on reference.)
pointIds.map { pointId =>
val range = pointId.id * vertexDim until (pointId.id + 1) * vertexDim
val sM: DenseVector[Double] = shape.vectorizer.vectorize(shapeMu(pointId.id))
shapeMuVec(range) := sM
val sE: DenseVector[Double] = expression.vectorizer.vectorize(expressMu(pointId.id))
expressMuVec(range) := sE
}
// W_tilde = [W_tilde1 W_tilde2]
// [W_tilde3 W_tilde4]
// sizes: W_tilde1: n_s x n_s
// W_tilde2: n_s x n_e
// W_tilde3: n_e x n_s
// W_tilde4: n_e x n_e
// n_s: PCABasisPart_s.cols()
// n_e: PCABasisPart_e.cols()
val n_S = shapeBasis.cols
val n_E = expressBasis.cols
val m_S = shapeBasis.rows
val w_tilde = DenseMatrix.zeros[Double](n_S + n_E, n_S + n_E)
// W_tilde1
w_tilde(0 until n_S, 0 until n_S) := shapeBasis.t * shapeBasis
// W_tilde2
w_tilde(0 until n_S, n_S until n_S + n_E) := shapeBasis.t * expressBasis
// W_tilde3
w_tilde(n_S until n_E + n_S, 0 until n_S) := expressBasis.t * shapeBasis
// W_tilde4
w_tilde(n_S until n_E + n_S, n_S until n_E + n_S) := expressBasis.t * expressBasis
// add sigma^2*I
val w_tilde_noise = w_tilde + DenseMatrix.eye[Double](n_S + n_E) * (shape.noiseVariance + expression.noiseVariance)
// WT = [ W_n^T ]
// [ W_e^T ]
val wt = DenseMatrix.zeros[Double](n_S + n_E, m_S)
wt(0 until n_S, 0 until m_S) := shapeBasis.t
wt(n_S until n_S + n_E, 0 until m_S) := expressBasis.t
CoeffsMatrices(breeze.linalg.inv(w_tilde_noise), wt)
}
/**
* Draw a sample from the model.
*
* @return
* a sample (shape and color)
*/
override def sample()(implicit rnd: Random): VertexColorMesh3D = {
VertexColorMesh3D(
discreteFieldToShape(shape.gpModel.sample(), expression.gpModel.sample()),
discreteFieldToColor(color.gpModel.sample())
)
}
/**
* Draw a set of coefficients from the prior of the statistical model.
*
* @return
* a set of coefficients to generate a random sample.
*/
override def sampleCoefficients()(implicit rnd: Random): MoMoCoefficients = {
MoMoCoefficients(shape.coefficientsDistribution.sample(),
color.coefficientsDistribution.sample(),
expression.coefficientsDistribution.sample()
)
}
/**
* Returns the same model but with exchanged or added landmarks.
*
* @param landmarksMap
* Map of named landmarks.
* @return
* Same model but with exchanged landmarks.
*/
override def withLandmarks(landmarksMap: Map[String, Landmark[_3D]]): MoMo =
MoMoExpress(referenceMesh, shape, color, expression, landmarksMap)
/** pad a coefficient vector if it is too short, basis with single vector */
override def padCoefficients(momoCoeff: MoMoCoefficients): MoMoCoefficients = {
def pad(coefficients: DenseVector[Double], rank: Int): DenseVector[Double] = {
require(coefficients.length <= rank, "too many coefficients for model")
require(rank == 0 || coefficients.length > 0, "coefficient vector cannot be empty")
if (coefficients.length == rank)
coefficients
else
DenseVector(coefficients.toArray ++ Array.fill(rank - coefficients.length)(0.0))
}
momoCoeff.copy(
shape = pad(momoCoeff.shape, shape.rank),
color = pad(momoCoeff.color, color.rank),
expression = pad(momoCoeff.expression, expression.rank)
)
}
override def zeroCoefficients: MoMoCoefficients = MoMoCoefficients(
DenseVector.zeros[Double](shape.rank),
DenseVector.zeros[Double](color.rank),
DenseVector.zeros[Double](expression.rank)
)
/**
* Reduces the rank of the model. Drops components only (pure truncation, no noise recalculation)
*
* @param shapeComps
* Number of shape components to keep.
* @param colorComps
* Number of color components to keep.
* @param expressComps
* Number of expression components to keep.
* @return
* Reduced model.
*/
def truncate(shapeComps: Int, colorComps: Int, expressComps: Int): MoMoExpress = {
require(shapeComps >= 0 && shapeComps <= shape.rank, "illegal number of reduced shape components")
require(colorComps >= 0 && colorComps <= color.rank, "illegal number of reduced color components")
require(expressComps >= 0 && expressComps <= expression.rank, "illegal number of reduced expression components")
MoMoExpress(referenceMesh,
shape.truncate(shapeComps),
color.truncate(colorComps),
expression.truncate(expressComps),
landmarks
)
}
// converters to deal with discrete fields
private def discreteFieldToShape(shapeField: DiscreteField[_3D, TriangleMesh, Point[_3D]],
expressionField: DiscreteField[_3D, TriangleMesh, EuclideanVector[_3D]]
): TriangleMesh3D = {
val points = shapeField.data.zip(expressionField.data).map { case (s, e) => s + e }
TriangleMesh3D(points, referenceMesh.triangulation)
}
private def discreteFieldToColor(colorField: DiscreteField[_3D, TriangleMesh, RGB]): SurfacePointProperty[RGBA] =
SurfacePointProperty(referenceMesh.triangulation, colorField.data.map(_.toRGBA))
private def shapeToDiscreteField(shape: TriangleMesh[_3D]): DiscreteField[_3D, TriangleMesh, Point[_3D]] =
DiscreteField(referenceMesh, shape.pointSet.points.toIndexedSeq)
private def colorToDiscreteField(color: SurfacePointProperty[RGBA]): DiscreteField[_3D, TriangleMesh, RGB] =
DiscreteField(referenceMesh, color.pointData.map(_.toRGB))
}
case class MoMoBasic(override val referenceMesh: TriangleMesh3D,
shape: PancakeDLRGP[_3D, TriangleMesh, Point[_3D]],
color: PancakeDLRGP[_3D, TriangleMesh, RGB],
override val landmarks: Map[String, Landmark[_3D]] = Map.empty[String, Landmark[_3D]]
) extends MoMo {
override def hasExpressions: Boolean = false
override def neutralModel: MoMoBasic = this
override def expressionModel: Option[MoMoExpress] = None
/**
* Returns the mean VertexColorMesh3D of the model.
*
* @return
* the mean
*/
override def mean: VertexColorMesh3D = {
val shapeInstance = discreteFieldToShape(shape.mean)
val colorInstance = discreteFieldToColor(color.mean)
VertexColorMesh3D(shapeInstance, colorInstance)
}
/**
* Generate model instance described by coefficients.
*
* @param coefficients
* model coefficients
* @return
* model instance as mesh with per vertex color
*/
override def instance(coefficients: MoMoCoefficients): VertexColorMesh3D = {
val MoMoCoefficients(shapeCoefficients, colorCoefficients, _) = padCoefficients(coefficients)
val shapeInstance = discreteFieldToShape(shape.instance(shapeCoefficients))
val colorInstance = discreteFieldToColor(color.instance(colorCoefficients))
VertexColorMesh3D(shapeInstance, colorInstance)
}
/**
* A fast method to evaluate a model instance at a specific point.
*
* @param coefficients
* model coefficients
* @param pid
* point-id
* @return
* shape and color values at point-id
*/
override def instanceAtPoint(coefficients: MoMoCoefficients, pid: PointId): (Point[_3D], RGB) = {
val MoMoCoefficients(shapeCoefficients, colorCoefficients, _) = padCoefficients(coefficients)
val point = shape.instanceAtPoint(shapeCoefficients, pid)
val colorAtPoint = color.instanceAtPoint(colorCoefficients, pid)
(point, colorAtPoint)
}
/**
* Calculates the parameters of the model representation of the sample.
*
* @param sample
* the sample (shape and color)
* @return
* the model coefficients
*/
override def coefficients(sample: VertexColorMesh3D): MoMoCoefficients = {
val shapeCoeffs = shape.coefficients(shapeToDiscreteField(sample.shape))
val colorCoeffs = color.coefficients(colorToDiscreteField(sample.color))
val expressCoeffs = DenseVector.zeros[Double](0)
MoMoCoefficients(shapeCoeffs, colorCoeffs, expressCoeffs)
}
/**
* Draw a sample from the model.
*
* @return
* a sample (shape and color)
*/
override def sample()(implicit rnd: Random): VertexColorMesh3D = {
VertexColorMesh3D(
discreteFieldToShape(shape.gpModel.sample()),
discreteFieldToColor(color.gpModel.sample())
)
}
/**
* Draw a set of coefficients from the prior of the statistical model.
*
* @return
* a set of coefficients to generate a random sample.
*/
override def sampleCoefficients()(implicit rnd: Random): MoMoCoefficients = {
MoMoCoefficients(
shape.coefficientsDistribution.sample(),
color.coefficientsDistribution.sample()
)
}
/**
* Returns the same model but with exchanged or added landmarks.
*
* @param landmarksMap
* Map of named landmarks.
* @return
* Same model but with exchanged landmarks.
*/
override def withLandmarks(landmarksMap: Map[String, Landmark[_3D]]): MoMo =
MoMoBasic(referenceMesh, shape, color, landmarksMap)
/** pad a coefficient vector if it is too short, basis with single vector */
override def padCoefficients(momoCoeff: MoMoCoefficients): MoMoCoefficients = {
def pad(coefficients: DenseVector[Double], rank: Int): DenseVector[Double] = {
require(coefficients.length <= rank, "too many coefficients for model")
require(rank == 0 || coefficients.length > 0, "coefficient vector cannot be empty")
if (coefficients.length == rank)
coefficients
else
DenseVector(coefficients.toArray ++ Array.fill(rank - coefficients.length)(0.0))
}
momoCoeff.copy(
shape = pad(momoCoeff.shape, shape.rank),
color = pad(momoCoeff.color, color.rank),
expression = DenseVector.zeros(0)
)
}
override def zeroCoefficients: MoMoCoefficients = MoMoCoefficients(
DenseVector.zeros[Double](shape.rank),
DenseVector.zeros[Double](color.rank),
DenseVector.zeros[Double](0)
)
/**
* Reduces the rank of the model. Drops components only (pure truncation)
*
* @param shapeComps
* Number of shape components to keep.
* @param colorComps
* Number of color components to keep.
* @return
* Reduced model.
*/
def truncate(shapeComps: Int, colorComps: Int): MoMoBasic = {
require(shapeComps >= 0 && shapeComps <= shape.rank, "illegal number of reduced shape components")
require(colorComps >= 0 && colorComps <= color.rank, "illegal number of reduced color components")
// @todo allow reduction with increasing noise to capture removed components
MoMoBasic(referenceMesh, shape.truncate(shapeComps), color.truncate(colorComps), landmarks)
}
// converters to deal with discrete fields
private def discreteFieldToShape(shapeField: DiscreteField[_3D, TriangleMesh, Point[_3D]]): TriangleMesh3D =
TriangleMesh3D(shapeField.data, referenceMesh.triangulation)
private def discreteFieldToColor(colorField: DiscreteField[_3D, TriangleMesh, RGB]): SurfacePointProperty[RGBA] =
SurfacePointProperty(referenceMesh.triangulation, colorField.data.map(_.toRGBA))
private def shapeToDiscreteField(shape: TriangleMesh[_3D]): DiscreteField[_3D, TriangleMesh, Point[_3D]] =
DiscreteField(referenceMesh, shape.pointSet.points.toIndexedSeq)
private def colorToDiscreteField(color: SurfacePointProperty[RGBA]): DiscreteField[_3D, TriangleMesh, RGB] =
DiscreteField(referenceMesh, color.pointData.map(_.toRGB))
}