Initialize a triangle mesh

18 views
Skip to first unread message

Maia R.

unread,
Jan 12, 2021, 2:47:45 PM1/12/21
to scalismo
Hi,
I have a question, both for Scala and Scalismo.

I have created a case class that needs a field of TriangleMesh type. As far as I know, class fields need to be initialized in Scala. How would you initialize a mesh as below ?
======
case class myClass(data: String) {

var referenceMesh: TriangleMesh[_3D] = ???

def setReferenceMesh(mesh: TriangleMesh[_3D]): Unit =  {
referenceMesh  = mesh
}

// other methods which use referenecMesh
def method1(): Unit = {
// use referenceMesh field here   

}

Thank you
Best regards

Marcel Luethi

unread,
Jan 13, 2021, 6:02:16 AM1/13/21
to Maia R., scalismo
Hi Maia

The ideal solution would be to pass the reference mesh in the constructor. This has the advantage, that the variable is never uninitialized and the object is always in a valid state. This would allow you do use a val instead of a var, which is preferred in Scala.

While it is possible to use setter methods in Scala, it is usually discouraged.  If you really have to use this approach, you can either initialize it with null or, preferably, use instead a variable of type Option[Triangle], which you initially set to None and then reset it to Some(mesh). This will avoid these nasty nullpointer exceptions and safe you a lot of trouble later. But as said, the best option is not to use var in the first place.

Best regards,

Marcel

--
You received this message because you are subscribed to the Google Groups "scalismo" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalismo+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalismo/4af297d7-0606-4068-9a18-4f15d28026f9n%40googlegroups.com.

Andreas Morel-Forster

unread,
Jan 13, 2021, 6:09:10 AM1/13/21
to scal...@googlegroups.com

A bit out of sync as I accidently replied privately earlier on instead on this list :-/

Hi,

Without knowing your application you havI think it would be better Scala if you would use a case class including the referenceMesh as a field as data:

case class MyClass(data: String, reference: TriangleMesh[_3D])

val instance: MyClass = ???
val newInstance = instance.copy(reference = newReference)


To create a dummy mesh without anything you can use the following code:

var mesh = TriangleMesh3D(IndexedSeq[Point3D](), TriangleList(IndexedSeq[TriangleCell]()))


Best, Andreas

V R

unread,
Jan 13, 2021, 8:05:44 AM1/13/21
to Andreas Morel-Forster, Marcel Luethi, scalismo
Hi, 
Thank you very much for all. I include  referenceMesh in my case class as Andreas has suggested and everything is working very well.
Best regards,
Maia

V R

unread,
Jan 13, 2021, 8:11:45 AM1/13/21
to Marcel Luethi, scalismo
Hi Marcel,
Thank you very much for the suggestion and explanation. I followed your advice and everything is working very well (sorry I didn't mention your solution earlier, I saw your message after Andreas's one).
Best regards,
Maia
Reply all
Reply to author
Forward
0 new messages