diff --git a/app/src/main/java/com/example/myhabitat/CreationOuvertureActivity.java b/app/src/main/java/com/example/myhabitat/CreationOuvertureActivity.java index d0cd5849400415a70e814a339dd67c1b908f8ef1..0dcde87baec7d80615c159e60107cdb54c9354c0 100644 --- a/app/src/main/java/com/example/myhabitat/CreationOuvertureActivity.java +++ b/app/src/main/java/com/example/myhabitat/CreationOuvertureActivity.java @@ -316,6 +316,8 @@ public class CreationOuvertureActivity extends AppCompatActivity{ Ouverture ouverture = new Ouverture(pieceDepart.getMurOrientation(orientationPieceDepart), pieceArrivee.getMurOrientation(orientationPieceArrivee), rectDepart, rectArrivee); habitat.addOuverture(ouverture); + Log.i("testECrea", habitat.getOuvertures().toString()); + Toast.makeText(getBaseContext(), "Ouverture créée !", Toast.LENGTH_SHORT).show(); enregistrement(); diff --git a/app/src/main/java/com/example/myhabitat/MainActivity.java b/app/src/main/java/com/example/myhabitat/MainActivity.java index b9f51d78b76d74e87b42a10977e203d21a542d0a..966a3356e7e32733fc0f70630b476583edc34a51 100644 --- a/app/src/main/java/com/example/myhabitat/MainActivity.java +++ b/app/src/main/java/com/example/myhabitat/MainActivity.java @@ -13,6 +13,7 @@ import habitat.Piece; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import outils.GrapheHabitat; import java.io.*; @@ -35,6 +36,10 @@ public class MainActivity extends AppCompatActivity { ouvrirJSON(); + //Graphe graphe = new Graphe(habitat); + GrapheHabitat grapheHabitat = new GrapheHabitat(habitat); + + Button b = findViewById(R.id.buttonImmersion); if(habitat.getPieces().size() == 0) { b.setEnabled(false); diff --git a/app/src/main/java/com/example/myhabitat/ModeConceptionActivity.java b/app/src/main/java/com/example/myhabitat/ModeConceptionActivity.java index f821d432360bc531f203fbf76158fd33e7309f43..a2120bb61880d89f341cb0f5361e97e8ba1d5f96 100644 --- a/app/src/main/java/com/example/myhabitat/ModeConceptionActivity.java +++ b/app/src/main/java/com/example/myhabitat/ModeConceptionActivity.java @@ -18,6 +18,8 @@ import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import habitat.*; +import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import outils.GestionnaireEditHabitat; @@ -67,7 +69,7 @@ public class ModeConceptionActivity extends AppCompatActivity { new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { - //On récupère les données de habitat + //On récupère les données de la photo Intent intent = result.getData(); if(intent != null) { Bundle extras = intent.getExtras(); @@ -106,6 +108,17 @@ public class ModeConceptionActivity extends AppCompatActivity { ); } + @Override + protected void onStart() { + Log.i("testOnStart", "je vais dans le OnStart"); + Log.i("testEConception", habitat.getOuvertures().toString()); + //On recupere habitat quand on revient de CreationOuvertureActivity + ouvrirJSON(); + Log.i("testEConception", habitat.getOuvertures().toString()); + + super.onStart(); + } + /** * Permet d'afficher l'ensemble des pieces de l'habitat */ @@ -263,4 +276,69 @@ public class ModeConceptionActivity extends AppCompatActivity { intent.putExtra("Habitat", habitat); startActivity(intent); } + + + /** + * Permet de recuperer l'habitat enregistre + */ + public void ouvrirJSON(){ + habitat = new Habitat(); + FileInputStream fis = null; + try { + fis = openFileInput("enregistrement.json"); + } catch (FileNotFoundException e) { + //throw new RuntimeException(e); + } + if (fis != null) { + String json = getFileContent(fis); + + try { + JSONObject enregistrement = new JSONObject(json); + JSONArray pieces = enregistrement.getJSONArray("Pieces"); + for(int i=0; i<pieces.length(); i++){ + JSONObject Jpiece = (JSONObject) pieces.get(i); + Piece piece = new Piece(Jpiece); + habitat.addPiece(piece); + } + JSONArray ouvertures = enregistrement.getJSONArray("Ouvertures"); + for(int i=0; i<ouvertures.length(); i++){ + JSONObject Jouverture = (JSONObject) ouvertures.get(i); + Ouverture ouverture = new Ouverture(Jouverture); + habitat.addOuverture(ouverture); + } + Log.i("testJSONouverture", habitat.toString()); + + } catch (JSONException e) { + //throw new RuntimeException(e); + } + + Log.i("testJSON", json); + }else{ + Log.i("testJSON", "pbm ouverture"); + } + //textView.setText(habitat.toString()); + } + + /** + * Fonction permettant de recuperer le texte dans un texte + * @param fis + * @return + */ + public String getFileContent(FileInputStream fis) { + StringBuilder sb = new StringBuilder(); + Reader r = null; + try { + r = new InputStreamReader(fis, "UTF-8"); + int ch = r.read(); + while(ch >= 0) { + sb.append((char)ch); + ch = r.read(); + } + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + return sb.toString(); + } } \ No newline at end of file diff --git a/app/src/main/java/habitat/Piece.java b/app/src/main/java/habitat/Piece.java index 80cc039e7453ffe40e520567c4aa53976bb6563a..9390bbb8a8799abb4e6a35610c27b95483d86fed 100644 --- a/app/src/main/java/habitat/Piece.java +++ b/app/src/main/java/habitat/Piece.java @@ -219,6 +219,6 @@ public class Piece implements Parcelable { */ @Override public int hashCode() { - return Objects.hash(nom, murs); + return Objects.hash(nom); } } diff --git a/app/src/main/java/outils/GrapheHabitat.java b/app/src/main/java/outils/GrapheHabitat.java new file mode 100644 index 0000000000000000000000000000000000000000..808df491388e6964f7759d49497d062f6ad18991 --- /dev/null +++ b/app/src/main/java/outils/GrapheHabitat.java @@ -0,0 +1,206 @@ +package outils; +import android.util.Log; +import habitat.Habitat; +import habitat.Ouverture; +import habitat.Piece; + +import java.util.*; + +public class GrapheHabitat { + + // A class to store a graph edge + class Edge + { + int source, dest, weight; + + public Edge(int source, int dest, int weight) + { + this.source = source; + this.dest = dest; + this.weight = weight; + } + } + + // A class to store a heap node + class Node + { + int vertex, weight; + + public Node(int vertex, int weight) + { + this.vertex = vertex; + this.weight = weight; + } + } + + // A class to represent a graph object + class Graph + { + // A list of lists to represent an adjacency list + List<List<Edge>> adjList = null; + + // Constructor + Graph(List<Edge> edges, int n) + { + adjList = new ArrayList<>(); + + for (int i = 0; i < n; i++) { + adjList.add(new ArrayList<>()); + } + + // add edges to the directed graph + for (Edge edge: edges) { + adjList.get(edge.source).add(edge); + } + } + } + + private Habitat habitat; + private HashMap<Integer, Piece> hmap; + private HashMap<Piece, Integer> hmapGet; + //private HashMap<Piece, List<Piece>> res; + private HashMap<Piece, HashMap<Piece, ArrayList<Piece>>> res; + + public GrapheHabitat(Habitat habitat) { + this.habitat = habitat; + res = new HashMap<>(); + //On initialise le num correspondant aux pieces + hmap = new HashMap<>(); + hmapGet = new HashMap<>(); + int i = 0; + for(Piece piece : habitat.getPieces()){ + hmap.put(i, piece); + hmapGet.put(piece, i); + i++; + + res.put(piece, new HashMap<>()); + } + + //Initialisation des resultats + for(Piece piece : habitat.getPieces()){ + for(Piece pieceArrivee : habitat.getPieces()){ + if(!pieceArrivee.equals(piece)){ + res.get(piece).put(pieceArrivee, new ArrayList<>()); + } + } + } + + + //On créé la list à partir des ouvertures + List<Edge> edges = new ArrayList<>(); + for(Ouverture ouverture : habitat.getOuvertures()){ + edges.add(new Edge(hmapGet.get(ouverture.getMurDepart().getPiece()), hmapGet.get(ouverture.getMurArrivee().getPiece()), 1)); + edges.add(new Edge(hmapGet.get(ouverture.getMurArrivee().getPiece()), hmapGet.get(ouverture.getMurDepart().getPiece()), 1)); + } + + + // total number of nodes in the graph (labelled from 0 to 4) + int n = habitat.getOuvertures().size()*2; + + // construct graph + Graph graph = new Graph(edges, n); + + + // run the Dijkstra’s algorithm from every node + for (int source = 0; source < n; source++) { + findShortestPaths(graph, source, n); + } + + //For test + /* + + for(Piece p1 : habitat.getPieces()){ + for(Piece p2 : habitat.getPieces()){ + if(!p1.equals(p2)){ + getPlusCourtChemin(p1, p2); + } + } + } + + */ + } + + private void getRoute(int[] prev, int i, List<Piece> route) + { + if (i >= 0) + { + getRoute(prev, prev[i], route); + route.add(hmap.get(i)); + } + } + + // Run Dijkstra’s algorithm on a given graph + public void findShortestPaths(Graph graph, int source, int n) + { + // create a min-heap and push source node having distance 0 + PriorityQueue<Node> minHeap; + minHeap = new PriorityQueue<>(Comparator.comparingInt(node -> node.weight)); + minHeap.add(new Node(source, 0)); + + // set initial distance from the source to `v` as infinity + List<Integer> dist; + dist = new ArrayList<>(Collections.nCopies(n, Integer.MAX_VALUE)); + + // distance from the source to itself is zero + dist.set(source, 0); + + // boolean array to track vertices for which minimum + // cost is already found + boolean[] done = new boolean[n]; + done[source] = true; + + // stores predecessor of a vertex (to a print path) + int[] prev = new int[n]; + prev[source] = -1; + + // run till min-heap is empty + while (!minHeap.isEmpty()) + { + // Remove and return the best vertex + Node node = minHeap.poll(); + + // get the vertex number + int u = node.vertex; + + // do for each neighbor `v` of `u` + for (Edge edge: graph.adjList.get(u)) + { + int v = edge.dest; + int weight = edge.weight; + + // Relaxation step + if (!done[v] && (dist.get(u) + weight) < dist.get(v)) + { + dist.set(v, dist.get(u) + weight); + prev[v] = u; + minHeap.add(new Node(v, dist.get(v))); + } + } + + // mark vertex `u` as done so it will not get picked up again + done[u] = true; + } + + List<Piece> routePiece = new ArrayList<>(); + + + + for (int i = 0; i < n; i++) + { + if (i != source && dist.get(i) != Integer.MAX_VALUE) + { + getRoute(prev, i, routePiece); + res.get(hmap.get(source)).get(hmap.get(i)).addAll(routePiece); + routePiece.clear(); + } + } + + + } + + + public void getPlusCourtChemin(Piece pieceDepart, Piece pieceArrivee){ + Log.i("testGraphe", "Path (" + pieceDepart.getNom() + " -> " + pieceArrivee.getNom() + ", Route = " + res.get(pieceDepart).get(pieceArrivee)); + } + +}