I don't know how exactly you had planned on working this and I don't know how you structured your data, so I'm just going to give you a hint to solve your current problem.

1
2
3
let convertToPoint3D (a : string) (b : string) (c : string) =
  try Some (Point3D (float a, float b, float c))
  with :? System.FormatException -> None

And of course:

1
convertToPoint3D : string -> string -> string -> Point3D option
By on 2/23/2012 12:51 AM ()

Hello Ramon,

Thanks for your prompt reply. Sorry for not being clear on what the usage of the data. I will give a brief infomation on what I am trying to achieve. Hope that will bring more clarity to my question.

Each line that start with "4" are points in 3D space will be then used plot a rectangle in WPF 3D, since there are 4 such lines it will draw a 3 dimensional cube.

Also that data does not look proper hence I have re-posted the data:

1
4 16 10 8 10 6 8 6 -6 8 6 -10 8 100 16 10 8 10 6 8 6 -6 8 6 -10 8 104 16 -10 8 10 -6 8 6 -6 8 -6 -10 8 -104 16 -10 8 -10 -6 8 -6 6 8 -6 10 8 -104 16 10 8 -10 6 8 -6 6 8 6 10 8 10

I hope this makes sense.

Thanks Muffadal

By on 2/23/2012 1:09 AM ()

I assume that you have line breaks in your data so i changed the string accordingly.
I don't have access to visual studio and WPF at the moment so i defined my own Point3D type for Try F#.
Is performance a factor and how certain are you that the incoming data is well formed, if not option types as mentioned above might be a good idea?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
type Point3D = Point of int*int*int
let data = "4 16 10 8 10 6 8 6 -6 8 6 -10 8 100\n16 10 8 10 6 8 6 -6 8 6 -10 8 10\n4 16 -10 8 10 -6 8 6 -6 8 -6 -10 8 -10\n4 16 -10 8 -10 -6 8 -6 6 8 -6 10 8 -10\n4 16 10 8 -10 6 8 -6 6 8 6 10 8 10"          
let lines = data.Split '\n'     |> Array.toList
                                |> List.map (fun e -> e.Split ' ' |> Array.toList)
                                |> List.filter (fun e -> (List.head e) = "4")
                                |> List.map (fun ls -> 
                                                match ls with
                                                |[]|[_]|[_;_]    -> failwith "DataError1"
                                                |x :: y :: xs    -> xs)                     
                                |> List.map (fun e -> e |> List.map int)
let points = 
    let parseLine ls = 
        let rec parse (l: int list) acc =
            match l with
            |[]|[_]|[_;_]            -> failwith "DataError2"
            |x :: y :: z :: []       -> Point (x,y,z) :: acc
            |x :: y :: z :: ps       -> parse ps (Point(x,y,z) :: acc)
        parse ls [] //reverse if necessary
    lines |> List.map parseLine
By on 2/23/2012 4:01 PM ()

Cleaned up your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
type Point3D = Point3D of int * int * int

let data = "4 16 10 8 10 6 8 6 -6 8 6 -10 8 100\n16 10 8 10 6 8 6 -6 8 6 -10 8 10\n4 16 -10 8 10 -6 8 6 -6 8 -6 -10 8 -10\n4 16 -10 8 -10 -6 8 -6 6 8 -6 10 8 -10\n4 16 10 8 -10 6 8 -6 6 8 6 10 8 10"

let lines =
  data.Split '\n'
  |> Array.toList
  |> List.map (fun e -> e.Split ' ' |> Array.toList)
  |> List.filter (function "4" :: _ -> true | _ -> false)
  |> List.map (function
               | _ :: _ :: xs -> List.map int xs
               | _ -> failwith "DataError1")

let points =
  let parseLine ls =
    let rec parse acc = function
      | [] -> acc
      | x :: y :: z :: xs -> parse (Point3D (x,y,z) :: acc) xs
      | _ -> failwith "DataError2"
    parse [] ls
    |> List.rev
  List.map parseLine lines
By on 2/24/2012 10:50 AM ()

Nice, thx.

By on 2/24/2012 11:10 AM ()

Thank Eugen, works perfectly. Just one thing I dont understand, the first 2 numbers are not the points but commands describing the what to draw (4 = rect & 16 = colour), but still the points come correctly, which portion of the code skips the first 2 numbers ?

Thanks,

Muffadal J.

By on 2/24/2012 8:18 AM ()

Line 8 checks if the list has at least three members.
Line 9 is where the list [x1;x2;xs] is mapped to xs and skips the first two numbers.

By on 2/24/2012 9:30 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper